Question

In: Computer Science

I have a python program that must check if an integer is the sum of the...

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

Solutions

Expert Solution

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:


Related Solutions

I'm writing a program that requires that I use the check sum technique with a for...
I'm writing a program that requires that I use the check sum technique with a for loop. A student enters their seven digit ID number. The seventh digit is determined from the other digits by this formula: 7th digit = (1 *(1st digit) + 2 * (2nd digit) + ... + 6 * (6th digit)) %10. The program should prompt users to enter a 7-digit number, and print valid if the actual 7th digit matches the computed 7th digit. Basicaly,...
Program must use Python 3 Your program must have a welcome message for the user. Your...
Program must use Python 3 Your program must have a welcome message for the user. Your program must have one class called CashRegister. Your program will have an instance method called addItem which takes one parameter for price. The method should also keep track of the number of items in your cart. Your program should have two getter methods. getTotal – returns totalPrice getCount – returns the itemCount of the cart Your program must create an instance of the CashRegister...
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i //...
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i // declare while loop condition where i is less then 25 // inside of brackets calculate the sum of i (addition) // increment i // outside the loop print the sum of values ============================================= 2) Create a sentinel value example if I press number 0 it will display the sum of data // create a scanner // prompt the user to to enter the numbers...
in python You will be writing a program that can be used to sum up and...
in python You will be writing a program that can be used to sum up and report lab scores. Your program must allow a user to enter points values for the four parts of the problem solving process (0-5 points for each step), the code (0-20 points), and 3 partner ratings (0-10) points each. It should sum the points for each problem-solving step, the code, and the average of the three partner ratings and print out a string reporting the...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
Write a Python program to find the smallest positive integer that is a perfect square and...
Write a Python program to find the smallest positive integer that is a perfect square and it contains exactly three different digits.
In python using tkinter, I want to create a program. The program can have various classes...
In python using tkinter, I want to create a program. The program can have various classes and methods but I want it to have circles, triangles, and squares. Each shape movies in a certain way, like circles can move linearly, triangles can be affected by gravity, and squares can only go up and down. If the shapes get too close to one another then they disappear and a new shape appears somewhere else. I want this program to run continuously.
Write a python function that accepts two integer values corresponding to the point (x, y). Check...
Write a python function that accepts two integer values corresponding to the point (x, y). Check whether the point is within the rectangle centered at (0, 0) with width 20 and height 15. For example, (-9, 7) is inside the rectangle and (11, 4) is outside the rectangle, as shown in the figure. Return True if the point falls within the rectangle and False otherwise
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT