is_gaussian_prime(z)?

23

Task

Write a function that accepts two integers a,b that represent the Gaussian integer z = a+ib (complex number). The program must return true or false depending on whether a+ib is a Gaussian prime or not.

Definition:

a + bi is a Gaussian prime if and only if it meets one of the following conditions:

  • a and b are both nonzero and a^2 + b^2 is prime
  • a is zero, |b| is prime and |b| = 3 (mod 4)
  • b is zero, |a| is prime and |a| = 3 (mod 4)

Details

You should only write a function. If your language does not have functions, you can assume that the integers are stored in two variables and print the result or write it to a file.

You cannot use built-in functions of your language like isprime or prime_list or nthprime or factor. The lowest number of bytes wins. The program must work for a,b where a^2+b^2 is a 32bit (signed) integer and should finish in not significantly more than 30 seconds.

Prime list

The dots represent prime numbers on the Gaussian plane (x = real, y = imaginary axis):

enter image description here

Some larger primes:

(9940, 43833)
(4190, 42741)
(9557, 41412)
(1437, 44090)

flawr

Posted 2014-08-07T15:54:54.570

Reputation: 40 560

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 and mF in CJam, ...) – None – 2014-08-07T16:24:01.617

Oh 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

Answers

4

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.

proud haskeller

Posted 2014-08-07T15:54:54.570

Reputation: 5 866

You can change notepad++ to LF (Unix) – MilkyWay90 – 2019-05-27T22:46:02.097

By my count, your first solution is actually 77 characters :D – killmous – 2014-08-07T20:32:58.600

i counted newlines, shouldn't i? – proud haskeller – 2014-08-07T20:34:09.590

I count 74 regular characters and 3 newlines – killmous – 2014-08-07T20:35:59.347

you're right, it seems that for some reason notepad++ adds characters before newlines. thanks! – proud haskeller – 2014-08-07T20:39:54.203

that's why I use sublime ;) glad to help! – killmous – 2014-08-07T20:40:51.150

Also I believe the arguments in your lambda \x->rem x n>0 are reversed. Try rem n x instead – killmous – 2014-08-07T20:47:33.873

thanks, i haven't caught that because i switched to it from ((\rem`n).(>0))` after running – proud haskeller – 2014-08-07T20:50:04.783

funny i removed the list comprehension for golfing one more char, as i usually hate them, and added them just for golfing. – proud haskeller – 2014-08-07T22:02:35.297

@proudhaskeller CRLF much? – nyuszika7h – 2014-08-08T09:04:12.563

are you talking about the character count thing? maybe, i don't know – proud haskeller – 2014-08-08T09:13:41.347

9

C, 149 118 characters

Edited version (118 characters):

int G(int a,int b){a=abs(a);b=abs(b);int n=a*b?a*a+b*b:a+b,
d=2;for(;n/d/d&&n%d;d++);return n/d/d|n<2?0:(a+b&3)>2|a*b;}

This is a single function:

  • G(a,b) returns nonzero (true) if a+bi is a Gaussian prime, or zero (false) otherwise.

It folds the integer primality test into an expression n/d/d|n<2 hidden in the return value calculation. This golfed code also makes use of a*b as a substitute for a&&b (in other words a!=0 && b!=0) and other tricks involving operator precedence and integer division. For example n/d/d is a shorter way of saying n/d/d>=1, which is an overflow-safe way of saying n>=d*d or d*d<=n or in essence d<=sqrt(n).


Original version (149 characters):

int Q(int n){int d=2;for(;n/d/d&&n%d;d++);return n/d/d||n<2;}
int G(int a,int b){a=abs(a);b=abs(b);return!((a|b%4<3|Q(b))*(b|a%4<3|Q(a))*Q(a*a+b*b));}

Functions:

  • Q(n) returns 0 (false) if n is prime, or 1 (true) if n is nonprime. It is a helper function for G(a,b).

  • G(a,b) returns 1 (true) if a+bi is a Gaussian prime, or 0 (false) otherwise.

Sample output (scaled up 200%) for |a|,|b| ≤ 128:

Sample128

Todd Lehman

Posted 2014-08-07T15:54:54.570

Reputation: 1 723

Can't you leave out the ints? – nyuszika7h – 2014-12-23T15:42:24.303

@feersum — Good lord, that's clever...especially the n>1>n/d/d. I just finally now saw this and haven't fully wrapped my mind around it. Will study. Thanks! – Todd Lehman – 2019-11-15T16:51:29.627

2+1 for the image! Could you also do one about the same size just in the first quadrant (because symmetry), it really looks great here=) – flawr – 2014-08-08T12:46:26.600

You can save a couple of characters by replacing d=2;for(;n/d/d&&n%d;d++); with for(d=2;n/d/d&&n%d++;); – Alchymist – 2014-08-16T22:20:56.540

@Alchymist — That indeed saves characters, but produces incorrect results. It's important that the d++ not happen as part of the condition, else it messes up the logic following. Also, moving the d=2 inside the for loop actually increases the character count rather than decreasing it, because d still needs to be declared as int prior to the for loop. Am I missing something? – Todd Lehman – 2014-08-16T23:09:50.457

Too true. Perils of looking at this away from a coding environment and not closely enough. Incrementing has to stay where it is and the initialisation only helps your original solution. There are obvious savings if you declare n & d outside the function, without specifying int, and initialise them in the for loop but I assume you are making the function self-contained which is a strict interpretation of the requirements. – Alchymist – 2014-08-21T22:29:38.980

1The prime-testing loop here is some spectacular golfing! However it is possible to achieve even more savings by dropping the int type specifiers for the return type and arguments, using a variable for |a| + |b| and optimizing the return statement: G(a,b){int s=abs(a)+abs(b),n=a*b?a*a+b*b:s,d=2;for(;n/d/d&&n%d;d++);return n>1>n/d/d&&s%4/3|a*b;} comes out to just 97 characters. – feersum – 2014-08-21T22:35:38.250

4

APL (Dyalog Unicode), 36 47 48 49 47 43 28 bytes

Takes an array of two integers a b and returns the Boolean value of the statement a+bi is a Gaussian integer.

Edit: +11 bytes because I misunderstood the definition of a Gaussian prime. +1 byte from correcting the answer again. +1 byte from a third bug fix. -2 bytes due to using a train instead of a dfn. -4 bytes thanks to ngn due to using a guard condition: if_true ⋄ if_false instead of if_true⊣⍣condition⊢if_false. -15 bytes thanks to ngn due to finding a completely different way to write the condition-if-else as a full train.

{2=≢∪⍵∨⍳⍵}|+.×0∘∊⊃|{⍺⍵}3=4||

Try it online!

Explanation

{2=≢∪⍵∨⍳⍵}|+.×0∘∊⊃|{⍺⍵}3=4||

                           |  ⍝ abs(a), abs(b) or abs(list)
                       3=4|   ⍝ Check if a and b are congruent to 3 (mod 4)
                  |{⍺⍵}       ⍝ Combine with (abs(a), abs(b))
              0∘∊⊃            ⍝ Pick out the original abs(list) if both are non-zero
                              ⍝ Else pick out (if 3 mod 4)
          |+.×                ⍝ Dot product with abs(list) returns any of
                              ⍝ - All zeroes if neither check passed
                              ⍝ - The zero and the number that IS 3 mod 4
                              ⍝ - a^2 + b^2
{2=≢∪⍵∨⍳⍵}                    ⍝ Check if any of the above are prime, and return

Sherlock9

Posted 2014-08-07T15:54:54.570

Reputation: 11 664

3

Python 2.7, 341 301 253 bytes, optimized for speed

lambda x,y:(x==0and g(y))or(y==0and g(x))or(x*y and p(x*x+y*y))
def p(n,r=[2]):a=lambda n:r+range(r[-1],int(n**.5)+1);r+=[i for i in a(n)if all(i%j for j in a(i))]if n>r[-1]**2else[];return all(n%i for i in r if i*i<n)
g=lambda x:abs(x)%4>2and p(abs(x))

Try it online!

#pRimes. need at least one for r[-1]
r=[2]
#list of primes and other to-check-for-primarity numbers 
#(between max(r) and sqrt(n))
a=lambda n:r+list(range(r[-1],int(n**.5)+1))
#is_prime, using a(n)
f=lambda n:all(n%i for i in a(n))
#is_prime, using r
def p(n):
    global r
    #if r is not enough, update r
    if n>r[-1]**2:
        r+=[i for i in a(n) if f(i)]
    return all(n%i for i in r if i*i<n)
#sub-function for testing (0,y) and (x,0)
g=lambda x:abs(x)%4==3 and p(abs(x))
#the testing function
h=lambda x,y:(x==0 and g(y)) or (y==0 and g(x)) or (x and y and p(x*x+y*y))

Thanks: 40+48 -- entire golfing to Jo King

Alexey Burdin

Posted 2014-08-07T15:54:54.570

Reputation: 844

The f lambda is also uneccesary, along with the list call. 257 bytes without those, plus some whitespace removal. This perhaps isn't as efficient anymore though

– Jo King – 2019-11-18T08:30:42.127

(15,0) is now true in 257 bytes version and the run time increased too 5.5s, sorry – Alexey Burdin – 2019-11-18T09:14:46.827

3

Haskell -- 121 chars (newlines included)

Here's a relatively simple Haskell solution that doesn't use any external modules and is golfed down as much as I could get it.

a%1=[]
a%n|n`mod`a<1=a:2%(n`div`a)|1>0=(a+1)%n
0#b=2%d==[d]&&d`mod`4==3where d=abs(b)
a#0=0#a
a#b=2%c==[c]where c=a^2+b^2

Invoke as ghci ./gprimes.hs and then you can use it in the interactive shell. Note: negative numbers are finicky and must be placed in parentheses. I.e.

*Main>1#1
True
*Main>(-3)#0
True
*Main>2#2
False

killmous

Posted 2014-08-07T15:54:54.570

Reputation: 369

3

Python - 121 120 chars

def p(x,s=2):
 while s*s<=abs(x):yield x%s;s+=1
f=lambda a,b:(all(p(a*a+b*b))if b else f(b,a))if a else(b%4>2)&all(p(b))

p checks whether abs(x) is prime by iterating over all numbers from 2 to abs(x)**.5 (which is sqrt(abs(x))). It does so by yielding x % s for each s. all then checks whether all the yielded values are non-zero and stops generating values once it encounters a divisor of x. In f, f(b,a) replaces the case for b==0, inspired by @killmous' Haskell answer.


-1 char and bugfix from @PeterTaylor

hlt

Posted 2014-08-07T15:54:54.570

Reputation: 181

Glad I could help :) – killmous – 2014-08-07T19:55:52.793

You could replace s<abs(x)**.5 with s*s<abs(x) for a saving of 2. Although really you should be checking <=, so it's probably buggy at present. – Peter Taylor – 2014-08-07T21:28:24.963

@PeterTaylor Thank you for pointing out the bug... – hlt – 2014-08-07T21:44:26.590

Calling f(0,15) yields TypeError: unsupported operand type(s) for &: 'bool' and 'generator' with my interpreter. :( – Falko – 2014-08-09T18:58:23.913

f(0,15) gives False for me, both on 2.7.6 and 3.4.1 (on OS X). What version are you on? – hlt – 2014-08-09T21:45:24.673

Python 2.7.8, OS X 10.9.4. Strange... Here it is working.

– Falko – 2014-08-09T22:19:07.597

2

Perl - 110 107 105 chars

I hope I followed the linked definition correctly...

sub f{($a,$b)=map abs,@_;$n=$a**(1+!!$b)+$b**(1+!!$a);(grep{$n%$_<1}2..$n)<2&&($a||$b%4>2)&&($b||$a%4>2)}

Ungolfed:

sub f {
  ($a,$b) = map abs, @_;
  $n = $a**(1+!!$b) + $b**(1+!!$a);
  (grep {$n%$_<1} 2..$n)<2 && ($a || $b%4==3) && ($b || $a%4==3)
}

Explanation, because someone asked: I read the arguments (@_) and put their absolute values into $a,$b, because the function does not need their sign. Each of the criteria requires testing a number's primality, but this number depends on whether $a or $b are zero, which I tried to express in the shortest way and put in $n. Finally I check whether $n is prime by counting how many numbers between 2 and itself divide it without a remainder (that's the grep...<2 part), and then check in addition that if one of the numbers is zero then the other one equals 3 modulo 4. The function's return value is by default the value of its last line, and these conditions return some truthy value if all conditions were met.

Tal

Posted 2014-08-07T15:54:54.570

Reputation: 1 358

I can't get it to work for negative parameters. – killmous – 2014-08-07T20:31:50.467

1@killmous you're right, just fixed it – Tal – 2014-08-07T20:33:34.997

may you explain the algorithm? – proud haskeller – 2014-08-07T22:03:33.217

1Nice! By the way, I think you could shave off a couple characters by writing $a%4>2 instead of $a%4==3. – Todd Lehman – 2014-08-07T23:01:08.257

2

golflua 147 141

The above count neglects the newlines that I've added to see the different functions. Despite the insistence to not do so, I brute-force solve primes within the cases.

\p(x)s=2@s*s<=M.a(x)?(x%s==0)~0$s=s+1$~1$
\g(a,b)?a*b!=0~p(a^2+b^2)??a==0~p(b)+M.a(b)%4>2??b==0~p(a)+M.a(a)%4>2!?~0$$
w(g(tn(I.r()),tn(I.r())))

Returns 1 if true and 0 if not.

An ungolfed Lua version,

-- prime number checker
function p(x)
   s=2
   while s*s<=math.abs(x) do
      if(x%s==0) then return 0 end
      s=s+1
   end
   return 1
end

-- check gaussian primes
function g(a,b)
   if a*b~=0 then
      return p(a^2+b^2)
   elseif a==0 then
      return p(b) + math.abs(b)%4>2
   elseif b==0 then
      return p(a) + math.abs(a)%4>2
   else
      return 0
   end
end


a=tonumber(io.read())
b=tonumber(io.read())
print(g(a,b))

Kyle Kanos

Posted 2014-08-07T15:54:54.570

Reputation: 4 270

Guess this dialect of Lua never caught on. Too bad. – ouflak – 2019-11-18T07:57:28.370

You can save 6 characters by just plugging tonumber(io.read()) as an argument to g in the end, and 2 more by removing the newlines – mniip – 2014-08-08T04:06:18.803

@mniip: the newlines were not counted, I just added those in for clarity (no sideways scrolling). I'll update the read in g when I get into work in a little bit. Thanks! – Kyle Kanos – 2014-08-08T10:52:41.210

Well does it still work in a reasonable amout of time for large numbers? I primarly thought about bruteforcing in the way of checking all gaussian integers a where |a| <= |z| if a | z (if a divides z). – flawr – 2014-08-08T15:01:16.357

@flawr: I tested it with a=2147483644, b=896234511 and got 0 in about 0.002 s. I also tested it with 2147483629 & 2147483587 (two very large primes) and got 0 in another 0.002 s. I'm trying to find a large pair of numbers such that a^2+b^2 is prime & ensure that I've got a working solution for such large numbers. – Kyle Kanos – 2014-08-08T15:09:56.363

@flawr: Tested with a=4600 & b=5603 (a^2+b^2=2147393609 is prime & < 2^32-1) and it took the same 0.002 seconds to return 1. Yay! – Kyle Kanos – 2014-08-08T19:41:21.840

1

APL(NARS), 99 chars, 198 bytes

r←p w;i;k
r←0⋄→0×⍳w<2⋄i←2⋄k←√w⋄→3
→0×⍳0=i∣w⋄i+←1
→2×⍳i≤k
r←1

f←{v←√k←+/2*⍨⍺⍵⋄0=⍺×⍵:(p v)∧3=4∣v⋄p k}

test:

  0 f 13
0
  0 f 9
0
  2 f 3
1
  3 f 4
0
  0 f 7
1
  0 f 9
0
  4600 f 5603
1  

RosLuP

Posted 2014-08-07T15:54:54.570

Reputation: 3 036

1

Runic Enchantments, 41 bytes

>ii:0)?\S:0)?\:*S:*+'PA@
3%4A|'S/;$=?4/?3

Try it online!

Ended up being a lot easier than I thought and wasn't much room for golfification. The original program I blocked out was:

>ii:0)?\S:0)?\:*S:*+'PA@
3%4A|'S/!   S/;$=

I played around with trying to compare both inputs at the same time (which saved all of one 1 byte), but when that drops into the "one of them is zero" section, there wasn't a good way to figure out which item was non-zero in order to perform the last check, much less a way to do it without spending at least 1 byte (no overall savings).

Draco18s no longer trusts SE

Posted 2014-08-07T15:54:54.570

Reputation: 3 053

1

Mathematica, 149 Characters

If[a==0,#[[3]]&&Mod[Abs@b,4]==3,If[b==0,#[[2]]&&Mod[Abs@a,4]==3,#[[1]]]]&[(q=#;Total[Boole@IntegerQ[q/#]&/@Range@q]<3&&q!=0)&/@{a^2+b^2,Abs@a,Abs@b}]

The code doesn't use any standard prime number features of mathematica, instead it counts the number of integers in the list {n/1,n/2,...,n/n}; if the number is 1 or 2, then n is prime. An elaborated form of the function:

MyIsPrime[p_] := (q = Abs@p; 
  Total[Boole@IntegerQ[q/#] & /@ Range@q] < 3 && q != 0)

Bonus plot of all the Gaussian Primes from -20 to 20:

Plot of gaussian primes

ralian

Posted 2014-08-07T15:54:54.570

Reputation: 181

1

Ruby -rprime, 65 60 80 bytes

Didn't notice the "can't use isPrime" rule...

->a,b{r=->n{(2...n).all?{|i|n%i>0}};c=(a+b).abs;r[a*a+b*b]||a*b==0&&r[c]&&c%4>2}

Try it online!

Value Ink

Posted 2014-08-07T15:54:54.570

Reputation: 10 608

0

Python - 117 122 121

def f(a,b):
 v=(a**2+b**2,a+b)[a*b==0]
 for i in range(2,abs(v)):
  if v%i<1:a=b=0
 return abs((a,b)[a==0])%4==3or a*b!=0

Falko

Posted 2014-08-07T15:54:54.570

Reputation: 5 307

Because 3 is the largest a number can be mod 4, you could replace the ==3 with >2 – FlipTack – 2019-10-19T11:27:40.103