Jump to content

Product of divisors: Difference between revisions

+add Pike
(Added XPL0 example.)
(+add Pike)
Line 1,197:
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%,12d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">r</span><span style="color: #0000FF;">}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))</span>
<!--</lang>-->
 
=={{header|Pike}}==
{{trans|Python}}
 
<lang Pike>int product_of_divisors(int n) {
int ans, i, j;
ans = i = j = 1;
 
while(i * i <= n) {
if(n%i == 0) {
ans = ans * i;
j = n / i;
if(j != i) {
ans = ans * j;
}
}
i = i+1;
}
 
return ans;
}
 
int main() {
int limit = 50;
write("Product of divisors for the first " + (string)limit + " positive integers:\n");
for(int i = 1; i < limit + 1; i++) {
write("%11d", product_of_divisors(i));
if(i%5 == 0) {
write("\n");
}
}
return 0;
}</lang>
 
{{out}}
<pre>
Product of divisors for the first 50 positive integers:
1 2 3 8 5
36 7 64 27 100
11 1728 13 196 225
1024 17 5832 19 8000
441 484 23 331776 125
676 729 21952 29 810000
31 32768 1089 1156 1225
10077696 37 1444 1521 2560000
41 3111696 43 85184 91125
2116 47 254803968 343 125000
</pre>
 
=={{header|Python}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.