In: Computer Science
I have a python program that must check if an integer is the sum of the squares of four consecutive prime numbers. However, when I run the program, it goes into an infinite while loop, and it doesn't give me any answer. I can only use the while, if, else, True and False commands. The code I'm using is
n1=2
n2=3
n3=5
n4=7
n = int(input("n: "))
if (n==(n1**2)+(n2**2)+(n3**2)+(n4**2)):
print(n1,n2,n3,n4)
else :
i = 2
pr = True
next = n4 + 2
while next <= n:
while i < next and pr == True:
if next%i == 0:
pr = False
else :
pr = True
i=i+1
if pr == True:
n1 = n2
n2 = n3
n3 = n4
n4 = next
if (n==(n1**2)+(n2**2)+(n3**2)+(n4**2)):
print(n1,n2,n3,n4)
else:
next = next + 2
if (next>n):
print("false")
And it only works with the number 87, since it's outside the while loop
ANSWER:
n1=2
n2=3
n3=5
n4=7
n = int(input("n: "))
if (n==(n1**2)+(n2**2)+(n3**2)+(n4**2)):
    print(n1,n2,n3,n4)
else :
    i = 2
    nextnum = n4 + 2
    while nextnum <= n:
        pr=True
        i=2
        while i < nextnum and pr==True:
            if nextnum%i == 0:
                pr = False
            i+=1
        if pr == True:
            n1 = n2
            n2 = n3
            n3 = n4
            n4 = nextnum
            if (n==(n1**2)+(n2**2)+(n3**2)+(n4**2)):
              print(n1,n2,n3,n4)
              break
            else:
              nextnum = nextnum + 2
            if (nextnum>n):
              print("false")
        else:
            nextnum = nextnum + 2
            if (nextnum>n):
              print("false")
NOTE: The above code is in Python3. Please refer to the attached screenshots for code indentation and sample I/O.

SAMPLE I/O:
