Haskell - 77/108107 Chars
usage:
in both the solutions, typing a%b will return whether a+bi is a gaussian prime.
the lowest i managed, but no creativity or performance (77 chars)
p n=all(\x->rem n x>0)[2..n-1]
a%0=rem a 4==3&&p(abs a)
0%a=a%0
a%b=p$a^2+b^2
this solution just powers through all numbers below n to check if it's prime.
ungolfed version:
isprime = all (\x -> rem n x != 0) [2..n-1] -- none of the numbers between 2 and n-1 divide n.
isGaussianPrime a 0 = rem a 4==3 && isprime (abs a)
isGaussianPrime 0 a = isGaussianPrime a 0 -- the definition is symmetric
isGaussianPrime a b = isprime (a^2 + b^2)
the next solution has an extra feature - memoization.
once you checked if some integer n is prime, you won't need to recalculate the "primeness" of all numbers smaller than or equal to n, as it will be stored in the computer.
(107 chars. the comments are for clarity)
s(p:x)=p:s[n|n<-x,rem n p>0] --the sieve function
l=s[2..] --infinite list of primes
p n=n==filter(>=n)l!!0 --check whether n is in the list of primes
a%0=rem a 4==3&&p(abs a)
0%a=a%0
a%b=p$a*a+b*b
ungolfed version:
primes = sieve [2..] where
sieve (p:xs) = p:filter (\n -> rem n p /= 0) xs
isprime n = n == head (filter (>=n) primes) -- checks if the first prime >= n is equal to n. if it is, n is prime.
isGaussianPrime a 0 = rem a 4==3 && isprime (abs a)
isGaussianPrime 0 a = isGaussianPrime a 0 -- the definition is symmetric
isGaussianPrime a b = isprime (a^2 + b^2)
this uses the sieve of Eratosthenes to compute an infinite list of all primes (called l for list in the code). (infinite lists are a well known trick of haskell).
how is it possible to have an infinite list?
at the start of the program, the list is unevaluated, and instead of storing the lists elements, the computer stores the way to compute them. but as the program accesses the list it partially evaluates itself up to the request. so, if the program were to request the fourth item in the list, the computer would compute all primes up to the forth that aren't already evaluated, store them, and the rest would remain unevaluated, stored as the way to compute them once needed.
note that all of this is given freely by the lazy nature of the Haskell language, none of that is apparent from the code itself.
both versions of the program are overloaded, so they can handle arbitrarily-sized data.
1 1073741857 not seems to me a Gaussian prime because 1^2+ 1073741857^2 is one even number... – RosLuP – 2019-10-19T20:45:06.177
@RosLuP Thanks, I made a mistake, I now added some hopefully correct examples. – flawr – 2019-10-19T21:47:51.987
$1^2+1026^2=1052677=6117257$, $1^2+1038^2=1077445=5215489$, $2^2+2051^2=4206605=5841321$, $17^2+1778^2=3161573=10133121$ TIO
– Alexey Burdin – 2019-11-18T04:16:26.470(9940, 43833), (4190, 42741), (9557, 41412), (1437, 44090)
are tested – Alexey Burdin – 2019-11-18T04:29:44.823@AlexeyBurdin Thanks, added! – flawr – 2019-11-18T07:51:02.473
2Are we allowed to use factorisation functions (
factor
in Bash,mf
andmF
in CJam, ...) – None – 2014-08-07T16:24:01.617Oh no, I forgot those factorizing methods existed, no please not=) And 32bit limit applies to a^2+b^2, would not make sense otherwise. Thank you for your inputs! I updated the question. – flawr – 2014-08-07T16:36:57.513
2I added a definition of Gaussian primes to the post. If you don't like how I've done it, feel free to roll it back, but I would definitely recommend including the definition somewhere. – undergroundmonorail – 2014-08-07T20:31:40.637
Thats nice, I originally just didn't want to directly point out how to determine the primality in order for people to get creative=) – flawr – 2014-08-07T22:31:04.560