Sphenic numbers: Difference between revisions

m
(→‎{{header|Phix}}: prepend Freepascal)
m (→‎{{header|AppleScript}}: Minor tidying.)
Line 54:
property numberList : prefabList(limit, mv)
end script
-- Write in 2, 3, and numbers which aren't their multiples.
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
Line 65 ⟶ 64:
set o's numberList's item n to mv
end repeat
-- "Cross out" slots for multiples of written-in numbers not then crossed out themselves.
repeat with n from 5 to (limit ^ 0.5 div 1) by 6
repeat with n from if5 to ((o'slimit numberList's^ item0.5) ndiv is n1) thenby 6
repeat with multiple from (n * n) to limit by2 ntimes
if (o's setnumberList's item multiplen of o's numberList= ton) mvthen
end repeat with multiple from (n * n) to limit by n
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 set n to n + 2
end ifrepeat
end repeat
return o's numberList's numbers
end sieveOfEratosthenes
Line 89 ⟶ 84:
end script
set counter|count| to 1
repeat until (counter|count| + counter|count| > |size|)
set o's lst to o's lst & o's lst
set counter|count| to counter|count| + counter|count|
end repeat
if (counter|count| < |size|) then set o's lst to o's lst & o's lst's items 1 thru (|size| - counter|count|)
return o's lst
end prefabList
Line 101 ⟶ 96:
set limit to limit - 1
script o
property primes : sieveOfEratosthenes(limit div 6(2 * 3))
property sphenics : prefabList(limit, missing value)
end script
set i to 0
repeat with a from 3 to (count o's primes)
Line 133 ⟶ 129:
on primeFactors(n)
set output to {}
if (n < 2) then return output
set limit to (n ^ 0.5) div 1
set if to 2
repeat until (if > limit)
if (n mod if = 0) then
repeatset whileend (nof modoutput ito is 0)f
set n to n div if
setrepeat endwhile of(n outputmod tof i= 0)
set n to n div f
end repeat
set end of output to i
if (limit > n) then set limit to n
end if
set if to if + 1
end repeat
if (limit < n) then set end of output to n
Line 153 ⟶ 151:
property sphenics : getSphenicsBelow(1000000)
end script
set {t1, t2, t3, t4, t5} to {{}, {}, count o's sphenics, 0, o's sphenics's item200000th 200000item}
repeat with i from 1 to ((count o's sphenics)t3 - 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)
Line 160 ⟶ 158:
if (s2 - s = 2) then
if (s2 < 10000) then ¬
set end of t2 to "{" & join({s, o's sphenics's itemitems i thru (i + 12), s2}, ", ") & "}"
set t4 to t4 + 1
if (t4 = 5000) then ¬
set t6 to "{" & join({s, o's sphenics's itemitems i thru (i + 12), s2}, ", ") & "}"
end if
end repeat
Line 178 ⟶ 176:
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>
</syntaxhighlight>
 
{{output}}
557

edits