Count in factors: Difference between revisions

Content added Content deleted
Line 940:
Print[Row[Riffle[Flatten[Map[Apply[ConstantArray, #] &, FactorInteger[n]]],"*"]]];
n++]</lang>
 
=={{header|Objeck}}==
<lang objeck>
class CountingInFactors {
function : Main(args : String[]) ~ Nil {
for(i := 1; i <= 10; i += 1;){
count := CountInFactors(i);
("{$i} = {$count}")->PrintLine();
};
for(i := 9991; i <= 10000; i += 1;){
count := CountInFactors(i);
("{$i} = {$count}")->PrintLine();
};
}
function : CountInFactors(n : Int) ~ String {
if(n = 1) {
return "1";
};
sb := "";
n := CheckFactor(2, n, sb);
if(n = 1) {
return sb;
};
n := CheckFactor(3, n, sb);
if(n = 1) {
return sb;
};
for(i := 5; i <= n; i += 2;) {
if(i % 3 <> 0) {
n := CheckFactor(i, n, sb);
if(n = 1) {
break;
};
};
};
return sb;
}
function : CheckFactor(mult : Int, n : Int, sb : String) ~ Int {
while(n % mult = 0 ) {
if(sb->Size() > 0) {
sb->Append(" x ");
};
sb->Append(mult);
n /= mult;
};
return n;
}
}
</lang>
Output:
<pre>
1 = 1
2 = 2
3 = 3
4 = 2 x 2
5 = 5
6 = 2 x 3
7 = 7
8 = 2 x 2 x 2
9 = 3 x 3
10 = 2 x 5
9991 = 97 x 103
9992 = 2 x 2 x 2 x 1249
9993 = 3 x 3331
9994 = 2 x 19 x 263
9995 = 5 x 1999
9996 = 2 x 2 x 3 x 7 x 7 x 17
9997 = 13 x 769
9998 = 2 x 4999
9999 = 3 x 3 x 11 x 101
10000 = 2 x 2 x 2 x 2 x 5 x 5 x 5 x 5
</pre>
 
=={{header|OCaml}}==