Forum Moderators: coopster
#!/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";
[edited by: rocknbil at 1:57 am (utc) on Mar 23, 2010]
$start = 1;
$end = 1000;
$divisor = 3;
$result = floor($end/$divisor)-floor($start/$divisor);
Getting out of here before I take another trip down the rabbit hole . . .
#!/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]