in reply to First code - Factors
You've already received some good comments. I'll just make a couple here:
Putting those together would give you:
foreach $int (1 .. ($no/2)) { if ($no%int == 0) { $div = $no / $int; print "$div\n"; } }
Algorithmically, you're doing quite a few operations that will *never* give you a factor, because your sequence includes even numbers other than two. You could generate a list that includes 2 and all the odd numbers up through half of the input number like this:
foreach $int (2, map {$_*2+1} (1 .. ($no/4)) { ....
I hope you find it helpful.
...roboticus
Update: If you want to crunch the code down, you could even combine most of it to a single statement:
print "$_\n" for grep { 0==$no%$_ } (2, map { 2*$_+1 } 1 .. $no/2);
Note: Another thing to watch for is that you're not restricting yourself to prime factors. I don't know if it matters to you, though.
|
|---|