Sphenic numbers: Difference between revisions
Content added Content deleted
(Added AppleScript.) |
|||
Line 42: | Line 42: | ||
* [[Square-free integers]] |
* [[Square-free integers]] |
||
<br> |
<br> |
||
=={{header|AppleScript}}== |
|||
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later |
|||
use framework "Foundation" -- For the sort in getSphenicsBelow(). |
|||
-- use scripting additions |
|||
on sieveOfEratosthenes(limit) |
|||
set mv to missing value |
|||
if (limit < 2) then return {} |
|||
script o |
|||
property numberList : prefabList(limit, mv) |
|||
end script |
|||
set o's numberList's second item to 2 |
|||
set o's numberList's third item to 3 |
|||
repeat with n from 5 to limit by 6 |
|||
set o's numberList's item n to n |
|||
tell (n + 2) to set o's numberList's item it to it |
|||
end repeat |
|||
repeat with n from (limit + 1) to ((count o's numberList) - 3) |
|||
set o's numberList's item n to mv |
|||
end repeat |
|||
repeat with n from 5 to (limit ^ 0.5 div 1) by 6 |
|||
if (o's numberList's item n is n) then |
|||
repeat with multiple from (n * n) to limit by n |
|||
set item multiple of o's numberList to mv |
|||
end repeat |
|||
end if |
|||
tell (n + 2) |
|||
if (o's numberList's item it is it) then |
|||
repeat with multiple from (it * it) to limit by it |
|||
set item multiple of o's numberList to mv |
|||
end repeat |
|||
end if |
|||
end tell |
|||
end repeat |
|||
return o's numberList's numbers |
|||
end sieveOfEratosthenes |
|||
on prefabList(|size|, filler) |
|||
if (|size| < 1) then return {} |
|||
script o |
|||
property lst : {filler} |
|||
end script |
|||
set counter to 1 |
|||
repeat until (counter + counter > |size|) |
|||
set o's lst to o's lst & o's lst |
|||
set counter to counter + counter |
|||
end repeat |
|||
if (counter < |size|) then set o's lst to o's lst & o's lst's items 1 thru (|size| - counter) |
|||
return o's lst |
|||
end prefabList |
|||
on getSphenicsBelow(limit) |
|||
set limit to limit - 1 |
|||
script o |
|||
property primes : sieveOfEratosthenes(limit div 6) |
|||
property sphenics : prefabList(limit, missing value) |
|||
end script |
|||
set i to 0 |
|||
repeat with a from 3 to (count o's primes) |
|||
set x to o's primes's item a |
|||
repeat with b from 2 to (a - 1) |
|||
set y to x * (o's primes's item b) |
|||
if (y ≥ limit) then exit repeat |
|||
repeat with c from 1 to (b - 1) |
|||
set z to y * (o's primes's item c) |
|||
if (z > limit) then exit repeat |
|||
set i to i + 1 |
|||
set o's sphenics's item i to z |
|||
end repeat |
|||
end repeat |
|||
end repeat |
|||
set o's sphenics to o's sphenics's items 1 thru i |
|||
return ((current application's class "NSArray"'s arrayWithArray:(o's sphenics))'s ¬ |
|||
sortedArrayUsingSelector:("compare:")) as list |
|||
end getSphenicsBelow |
|||
on join(lst, delim) |
|||
set astid to AppleScript's text item delimiters |
|||
set AppleScript's text item delimiters to delim |
|||
set txt to lst as text |
|||
set AppleScript's text item delimiters to astid |
|||
return txt |
|||
end join |
|||
on primeFactors(n) |
|||
set output to {} |
|||
set limit to (n ^ 0.5) div 1 |
|||
set i to 2 |
|||
repeat until (i > limit) |
|||
if (n mod i = 0) then |
|||
repeat while (n mod i is 0) |
|||
set n to n div i |
|||
end repeat |
|||
set end of output to i |
|||
if (limit > n) then set limit to n |
|||
end if |
|||
set i to i + 1 |
|||
end repeat |
|||
if (limit < n) then set end of output to n |
|||
return output |
|||
end primeFactors |
|||
on task() |
|||
script o |
|||
property sphenics : getSphenicsBelow(1000000) |
|||
end script |
|||
set {t1, t2, t3, t4, t5} to {{}, {}, count o's sphenics, 0, o's sphenics's item 200000} |
|||
repeat with i from 1 to ((count o's sphenics) - 2) |
|||
set s to o's sphenics's item i |
|||
if (s < 1000) then set end of t1 to text -4 thru -1 of (" " & s) |
|||
set s2 to o's sphenics's item (i + 2) |
|||
if (s2 - s = 2) then |
|||
if (s2 < 10000) then ¬ |
|||
set end of t2 to "{" & join({s, o's sphenics's item (i + 1), s2}, ", ") & "}" |
|||
set t4 to t4 + 1 |
|||
if (t4 = 5000) then ¬ |
|||
set t6 to "{" & join({s, o's sphenics's item (i + 1), s2}, ", ") & "}" |
|||
end if |
|||
end repeat |
|||
set output to {"Sphenic numbers < 1,000:"} |
|||
repeat with i from 1 to 135 by 15 |
|||
set end of output to join(t1's items i thru (i + 14), "") |
|||
end repeat |
|||
set end of output to linefeed & "Sphenic triplets < 10,000:" |
|||
repeat with i from 1 to 21 by 3 |
|||
set end of output to join(t2's items i thru (i + 2), " ") |
|||
end repeat |
|||
set end of output to linefeed & "There are " & t3 & " sphenic numbers < 1,000,000" |
|||
set end of output to "There are " & t4 & " sphenic triplets < 1,000,000" |
|||
set end of output to "The 200,000th sphenic number is " & t5 & ¬ |
|||
" (" & join(primeFactors(t5), " * ") & ")" |
|||
set end of output to "The 5,000th sphenic triplet is " & t6 |
|||
return join(output, linefeed) |
|||
end task |
|||
task() |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<syntaxhighlight lang="applescript">"Sphenic numbers < 1,000: |
|||
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174 |
|||
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285 |
|||
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406 |
|||
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494 |
|||
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609 |
|||
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678 |
|||
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795 |
|||
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902 |
|||
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994 |
|||
Sphenic triplets < 10,000: |
|||
{1309, 1310, 1311} {1885, 1886, 1887} {2013, 2014, 2015} |
|||
{2665, 2666, 2667} {3729, 3730, 3731} {5133, 5134, 5135} |
|||
{6061, 6062, 6063} {6213, 6214, 6215} {6305, 6306, 6307} |
|||
{6477, 6478, 6479} {6853, 6854, 6855} {6985, 6986, 6987} |
|||
{7257, 7258, 7259} {7953, 7954, 7955} {8393, 8394, 8395} |
|||
{8533, 8534, 8535} {8785, 8786, 8787} {9213, 9214, 9215} |
|||
{9453, 9454, 9455} {9821, 9822, 9823} {9877, 9878, 9879} |
|||
There are 206964 sphenic numbers < 1,000,000 |
|||
There are 5457 sphenic triplets < 1,000,000 |
|||
The 200,000th sphenic number is 966467 (17 * 139 * 409) |
|||
The 5,000th sphenic triplet is {918005, 918006, 918007}"</syntaxhighlight> |
|||
=={{header|J}}== |
=={{header|J}}== |
||