Ask Your Question

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

asked 2022-03-02 06:22:25 +0100

brennan gravatar image

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 flag offensive close merge delete



use prime_range

FrédéricC gravatar imageFrédéricC ( 2022-03-02 08:30:46 +0100 )edit

1 Answer

Sort by » oldest newest most voted

answered 2022-03-02 07:54:45 +0100

rburing gravatar image

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.

edit flag offensive delete link more



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

for pV in range(1,10):
    for qV in range(1,10):
        pV=Integer(pV) ; qV=Integer(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)
ortollj gravatar imageortollj ( 2022-03-02 08:45:26 +0100 )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().

rburing gravatar imagerburing ( 2022-03-02 09:59:23 +0100 )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 !

ortollj gravatar imageortollj ( 2022-03-02 10:20:37 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2022-03-02 06:22:25 +0100

Seen: 176 times

Last updated: Mar 02 '22