Forum Moderators: coopster

Message Too Old, No Replies

Neep Help in the Loop Department

Just some basic C++

         

Mrs_BunBun

11:32 pm on Mar 22, 2010 (gmt 0)

10+ Year Member



Just some basic c++ stuff - since I'm definately new in this field - and hopefully I'm posting this in the right area.

In any case, I want to create a program that will tell me how many numbers from 1 - 1000 are divisible by 3

In this program I will need to use a function to 1) use the divisibility property to add the digits of each integer to see if it's divisble by 3

ie: 36
3 + 6 = 9
9 / 3 = 3 with no remainders hence it's divisble by 3

I know its a completely roundabout way of finding it, but I need help in hte loop department and it's a good way to practice - I suppose >.>

So far I found out how to seperate the numbers from each number (ie: 34 - 3 4)
And I would assume that we would have to use a for loop - but from there I really get stumped!

Help would be appreciated!

rocknbil

1:20 am on Mar 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Mrs_BunBun!

The modulus operator % is your friend (in any language.) :-) Modulus will tell you if it's divisible by [any number]. MY C is far too rusty to take a stab at it.

This is a perl example as I'm not in an uploadable/testable location ATM, but you can see what's doing the grunt work, it's almost identical to what you'd do in PHP.


#!/usr/bin/perl
print "Numbers from 1-1000 divisibly by three\n";
$top = 1000;
for ($i=1;$i<=$top;$i++) {
$sum=0;
# PHP: $nums = explode('',$i);
@nums = split('',$i);
# PHP: foreach ($nums as $j) { $sum += $j; }
foreach $j (@nums) { $sum += $j; }
if ($sum % 3 == 0) {
print "Num: $i Sum: $sum Divided by 3: " . $sum/3 . "\n";
# so you can see what's up
sleep 1;
}
}
print "\n\ndone";


Starts off with 3, 6, 9, 12, 18, 21, 24 . . . oddly enough (or, maybe not so oddly for math aficionados) those numbers themselves are also divisible by three . . . or maybe that's the point of the exercise, don't know. :-)

18 / 3 = 6
1+8 = 9 / 3 = 3

Getting out of here before I take another trip down the rabbit hole . . .

[edited by: rocknbil at 1:57 am (utc) on Mar 23, 2010]

eelixduppy

1:38 am on Mar 23, 2010 (gmt 0)



I don't know if you just want to make this script just to play around to learn or not, however, if you actually need to use this in practice there are MUCH faster ways of calculating this value, and it can be done in one simple line.


$start = 1;
$end = 1000;
$divisor = 3;
$result = floor($end/$divisor)-floor($start/$divisor);

rocknbil

1:47 am on Mar 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At first I was "huh?" but then realized,

tell me how many numbers from 1 - 1000 are divisible by 3


Which floor does, but

use the divisibility property to add the digits of each integer to see if it's divisble by 3


Sent me off to the Tea Party. :-)

rocknbil

2:14 am on Mar 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Getting out of here before I take another trip down the rabbit hole . . .


Too late . . . started watching the number sequences output by this pattern, a revision of the above that tracks the maximum number of [sum-of-digits] that are divisible by 3 and the resulting quotient, and although that quotient is not always divisible by 3 (7, 4, etc.) the maximum of both are 27 and 9, and 9 * 3 = 27. What a spin.

And . . . the quotient never exceeds 9, the sum of numbers never exceeds 27, even setting $top to a higher number. Tested 2000, 5000 as of this post, 2000 returns 666 results. :-) Edit: the magic fails (sorta) over 2000, maxnums and quotient are 30 and 10.

Probably fourth grade math for most of you, it's too far behind me . . .


#!/usr/bin/perl
print "Numbers from 1-1000 divisible by three\n";
$top = 1000;
$sectioncount=$maxnum=$maxsum=0;
for ($i=1;$i<=$top;$i++) {
$sum=0;
# PHP: $nums = explode('',$i);
@nums = split('',$i);
# PHP: foreach ($nums as $j) { $sum += $j; }
foreach $j (@nums) { $sum += $j; }
if ($sum % 3 == 0) {
$count++;
$sectioncount++;
$maxnum = (($sum/3) > $maxnum)?$sum/3:$maxnum;
$maxsum = ($sum > $maxsum)?$sum:$maxsum;
print "Linecount: $count Num: $i Sum: $sum Divided by 3: " . $sum/3 . "\n";
# so you can see what's up
if ($sectioncount >=25) {
print "\n";
sleep 2;
$sectioncount=0;
}
}
}
print "\n\ndone. Maximum number of the sum of numbers/3 was $maxnum.\n";
print "Maximum sum of numbers is $maxsum\n";

[edited by: rocknbil at 2:23 am (utc) on Mar 23, 2010]

eelixduppy

2:17 am on Mar 23, 2010 (gmt 0)



If you really want to go on a trip, write this loop without the use of "split". ;)

...at least that's how it would have to be done in C, dividing by 10 to get each digit.

rocknbil

2:25 am on Mar 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What, I shouldn't use string operators on numbers, why not? It's every bit as good as eval on strings . . . (KIDDING!)