Perfect numbers: Difference between revisions

Content added Content deleted
(→‎{{header|Picat}}: Split into subsections, fixed link)
Line 2,900: Line 2,900:


=={{header|Picat}}==
=={{header|Picat}}==
First is the slow perfect1/1 that use the simple divisors/1 function:
===Simple divisors/1 function===
First is the slow <code>perfect1/1</code> that use the simple divisors/1 function:
<lang Picat>go =>
<lang Picat>go =>
println(perfect1=[I : I in 1..10_000, perfect1(I)]),
println(perfect1=[I : I in 1..10_000, perfect1(I)]),
Line 2,911: Line 2,912:
<pre>perfect1 = [1,6,28,496,8128]</pre>
<pre>perfect1 = [1,6,28,496,8128]</pre>


===Using formula for perfect number candidates===

The formula for perfect number candidates is: 2^(p-1)*(2^p-1) for prime p. This is used to find some more perfect numbers in reasonable time. <code>perfect2/1</code> is a faster version of checking if a number is perfect.

The formula for perfect number candidates is: 2^(p-1)*(2^p-1) for prime p. This is used to find some more perfect numbers in reasonable time. perfect2/1 is a faster version of checking if a number is perfect.
<lang Picat>go2 =>
<lang Picat>go2 =>
println("Using the formula: 2^(p-1)*(2^p-1) for prime p"),
println("Using the formula: 2^(p-1)*(2^p-1) for prime p"),
Line 2,967: Line 2,967:
CPU time 118.039 seconds. Backtracks: 0</pre>
CPU time 118.039 seconds. Backtracks: 0</pre>


===Using list of the prime generating the perfect numbers===
Now let's cheat a little. At [[Media:https://en.wikipedia.org/wiki/Perfect_number]] there is a list of the first 48 primes that generates perfect numbers according to the formula 2^(p-1)*(2^p-1) for prime p.
Now let's cheat a little. At https://en.wikipedia.org/wiki/Perfect_number there is a list of the first 48 primes that generates perfect numbers according to the formula 2^(p-1)*(2^p-1) for prime p.


The perfect numbers are printed only if they has < 80 digits, otherwise the number of digits are shown. The program stops when reaching a number with more than 100 000 digits. (Note: The major time running this program is getting the number of digits.)
The perfect numbers are printed only if they has < 80 digits, otherwise the number of digits are shown. The program stops when reaching a number with more than 100 000 digits. (Note: The major time running this program is getting the number of digits.)

<lang Picat>go3 =>
<lang Picat>go3 =>
ValidP = [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607,
ValidP = [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607,
Line 3,026: Line 3,026:
prime 132049: len = 79502
prime 132049: len = 79502
prime 216091: len = 130100</pre>
prime 216091: len = 130100</pre>



=={{header|PicoLisp}}==
=={{header|PicoLisp}}==