# primes of the form (p^q-q^p)/2 bad syntax?

I am trying to make a really simple code to get a list of primes n of the form (p^q-q^p)/2 where p and q are also prime but something weird is happening. I am using the code:

....: for p in srange(1,100):

....: for q in srange(1,100):

....: if p.is_prime() and q.is_prime() and ((p^q-q^p)/2).is_prime():

....: print(p,q)

But Sage returns no results. But if we let p=3 and q=5, then (3^5-5^3)/2=59 which is prime. What am I doing wrong?!?!? (besides the terrible brute force code lol)

edit retag close merge delete

1

use prime_range

( 2022-03-02 08:30:46 +0200 )edit

Sort by » oldest newest most voted

Replace the final condition by (p^q-q^p) % 2 == 0 and ((p^q-q^p)//2).is_prime(), so that integer division is used and hence the is_prime method of an integer is called, rather than the is_prime method of a rational number.

more

@rburing

I suppose it is a bug that fNum.is_prime() generates an error ?

var('p,q',domain='integer')
f=(p^q-q^p)/2
for pV in range(1,10):
for qV in range(1,10):
pV=Integer(pV) ; qV=Integer(qV)
fNum=f.subs(p=pV,q=qV)
if pV.is_prime() and qV.is_prime()  :
if fNum.is_integer() and fNum.is_positive() :
show(fNum," \t p : \t ",pV," \t q : \t ",qV)
#show(fNum.is_prime())

( 2022-03-02 08:45:26 +0200 )edit

@ortollj The error shows that the is_prime method of a symbolic expression (here: fNum) does not have a sensible implementation. I would consider it mostly expected, though I would probably prefer an immediate NotImplementedError. You can do Integer(fNum).is_prime(), after checking e.g. fNum.is_integer().

( 2022-03-02 09:59:23 +0200 )edit

OK, but it is a bit weird that after testing with success fNum is integer ,we need to cast it as Integer ? because fNum.is_integer()=True tell us it is an Integer !

( 2022-03-02 10:20:37 +0200 )edit