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
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...
The Sum and The Average In C++, Write a program that reads in 10 integer numbers....
The Sum and The Average In C++, Write a program that reads in 10 integer numbers. Your program should do the following things: Use a Do statement Determine the number positive or negative Count the numbers of positive numbers, and negative Outputs the sum of: all the numbers greater than zero all the numbers less than zero (which will be a negative number or zero) all the numbers Calculate the average of all the numbers. The user enters the ten...
4. Write a program to compute the sum of digits of an integer. For example, given...
4. Write a program to compute the sum of digits of an integer. For example, given the integer 35931, your program should output 21. (21 is the sum 3 + 5 + 9 + 3 + 1) PYTHON
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...
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....
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?
1. Write a program that read a sequence of positive integer inputs and print the sum...
1. Write a program that read a sequence of positive integer inputs and print the sum and average of the inputs. Assume that the user always enters a positive number (integer) as an input value or an empty space as a sentinel value. Please use the below to test your code [45 points]: Enter an int value or empty string to end: 98 Enter an int value or empty string to end: 78 Enter an int value or empty string...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT