Perfect numbers: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|AppleScript}}: →Idiomatic: Tidied. Added Euclid method.) |
||
Line 718: | Line 718: | ||
---- |
---- |
||
===Idiomatic=== |
===Idiomatic=== |
||
====Sum of proper divisors==== |
|||
⚫ | |||
<lang applescript>on aliquotSum(n) |
|||
-- Joining the dots in the Wikipedia entry suggests that all perfect numbers |
|||
⚫ | |||
-- identifiable by AppleScript end with either 6 or 8. Testing for this |
|||
-- immediately eliminates negatives, fractional numbers, and 4/5 of what's left. |
|||
tell (n mod 10) to if not ((it = 6) or (it = 8)) then return false |
|||
set sum to 1 |
set sum to 1 |
||
set sqrt to n ^ 0.5 |
set sqrt to n ^ 0.5 |
||
Line 731: | Line 729: | ||
end if |
end if |
||
repeat with i from 2 to limit |
repeat with i from 2 to limit |
||
if (n mod i is 0) then |
if (n mod i is 0) then set sum to sum + i + n div i |
||
⚫ | |||
⚫ | |||
⚫ | |||
end repeat |
end repeat |
||
return |
return sum |
||
end aliquotSum |
|||
on isPerfect(n) |
|||
⚫ | |||
-- All the known perfect numbers listed in Wikipedia end with either 6 or 28. |
|||
-- These endings are either preceded by odd digits or are the numbers themselves. |
|||
tell (n mod 10) to ¬ |
|||
return ((((it = 6) and ((n mod 20 = 16) or (n = 6))) or ¬ |
|||
((it = 8) and ((n mod 200 = 128) or (n = 28)))) and ¬ |
|||
(my aliquotSum(n) = n)) |
|||
end isPerfect |
end isPerfect |
||
local output, n |
local output, n |
||
set output to {} |
set output to {} |
||
repeat with n from 1 to |
repeat with n from 1 to 10000 |
||
if (isPerfect(n)) then set end of output to n |
if (isPerfect(n)) then set end of output to n |
||
end repeat |
end repeat |
||
Line 750: | Line 755: | ||
<lang applescript>{6, 28, 496, 8128}</lang> |
<lang applescript>{6, 28, 496, 8128}</lang> |
||
====Euclid==== |
|||
But for practical purposes, since only the first 7 of the known perfect numbers can be accurately represented by AppleScript numbers, a simple look-up table would be the best approach: |
|||
<lang applescript>on isPerfect(n) |
<lang applescript>on isPerfect(n) |
||
-- All the known perfect numbers listed in Wikipedia end with either 6 or 28. |
|||
tell (n mod 10) to if not ((it = 6) or (it = 8)) then return false |
|||
-- These endings are either preceded by odd digits or are the numbers themselves. |
|||
⚫ | |||
tell (n mod 10) to ¬ |
|||
⚫ | |||
if not (((it = 6) and ((n mod 20 = 16) or (n = 6))) or ((it = 8) and ((n mod 200 = 128) or (n = 28)))) then ¬ |
|||
return false |
|||
-- Work through the only seven primes p where (2 ^ p - 1) is also prime |
|||
-- and (2 ^ p - 1) * (2 ^ (p - 1)) is a number that AppleScript can handle. |
|||
repeat with p in {2, 3, 5, 7, 13, 17, 19} |
|||
tell (2 ^ p - 1) * (2 ^ (p - 1)) |
|||
if (it < n) then |
|||
else |
|||
⚫ | |||
⚫ | |||
end tell |
|||
end repeat |
|||
return missing value |
|||
end isPerfect |
end isPerfect |
||
Line 761: | Line 778: | ||
set output to {} |
set output to {} |
||
repeat with n from 2 to 33551000 by 2 |
repeat with n from 2 to 33551000 by 2 |
||
if (isPerfect(n) |
if (isPerfect(n)) then set end of output to n |
||
end repeat |
end repeat |
||
return output</lang> |
return output</lang> |
||
Line 767: | Line 784: | ||
{{output}} |
{{output}} |
||
<lang applescript>{6, 28, 496, 8128, 33550336}</lang> |
<lang applescript>{6, 28, 496, 8128, 33550336}</lang> |
||
====Practical==== |
|||
But since AppleScript can only physically manage seven of the known perfect numbers, they may as well be in a look-up list for maximum efficiency: |
|||
⚫ | |||
if (n > 1.37438691328E+11) then return missing value -- Too high for perfection to be determinable. |
|||
⚫ | |||
end isPerfect</lang> |
|||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |