Last active
August 21, 2018 08:32
-
-
Save ChristoferK/df7433e4b4c125438ca9182f6c3cd767 to your computer and use it in GitHub Desktop.
[Prime Numbers] 1a. A superfast prime number generator that lists all primes up to and including N. 1b. A JavaScript prime number generating one-liner. 2. A fast integer factorisation handler that utilises the prime number generator. #AppleScript #JavaScript #primes #lists #math #integers #factorisation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
to factorise(x) | |
script | |
property lim : x ^ 0.5 | |
property P : primes(lim) & getNextPrime(lim) | |
property L : {} | |
end script | |
tell the result | |
repeat with p0 in its P | |
repeat until (x mod p0) ≠ 0 | |
set end of its L to p0 | |
set x to x / p0 | |
end repeat | |
if x = 1 then exit repeat | |
end repeat | |
if x ≠ 1 then set end of its L to x as integer | |
contents of its L | |
end tell | |
end factorise | |
to getNextPrime(x) | |
local x | |
if x < 2 then return 2 | |
script | |
property P : primes(x) | |
on isPrime(n) | |
local n | |
set lim to n ^ 0.5 | |
repeat with p0 in P | |
if (n mod p0) = 0 then return false | |
if p0 > lim then exit repeat | |
end repeat | |
true | |
end isPrime | |
end script | |
tell the result | |
set x to end of its P | |
repeat | |
set x to x + 1 | |
if isPrime(x) then exit repeat | |
end repeat | |
end tell | |
x | |
end getNextPrime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on primes(N) | |
local N | |
if N < 2 then return | |
script P -- The list of integers to be sieved | |
on Array(N) -- Generate a list of integers from 1 to n | |
local N | |
if N < 1 then return {} | |
script A | |
property L : {} | |
end script | |
repeat with x from 1 to N | |
set end of A's L to x | |
end repeat | |
A's L | |
end Array | |
script Q | |
property fn : Array | |
property L : my fn(N) | |
end script | |
property L : rest of Q's L | |
end script | |
tell P to repeat with i from 1 to (N ^ 0.5) | |
set d to item i of its L | |
if d ≠ null then repeat with j from ¬ | |
(i + d) to the length of its L by d | |
set item j of its L to null | |
end repeat | |
end repeat | |
numbers of P's L | |
end primes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
primes = N => Array.apply(null,Array(N)) | |
.map((x,y) => y) | |
.filter(i => (i > 1) && Array.apply(null,Array(1 + ~~Math.sqrt(i))) | |
.every((x,y) => (y < 2) || (i % y !== 0))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment