Finding if n! + 1 is a perfect square

PythonNewbie

I'm trying to write a program to look for a number, n, between 0 and 100 such that n! + 1 is a perfect square. I'm trying to do this because I know there are only three so it was meant as a test of my Python ability.

Refer to Brocard's problem.

samgak

For very large numbers it's better to avoid using floating point square roots altogether because you will run into too many precision issues and you can't even guarantee that you will be within 1 integer value of the correct answer. Fortunately Python natively supports integers of arbitrary size, so you can write an integer square root checking function, like this:

def isSquare(x):
    if x == 1:
        return True
    low = 0
    high = x // 2
    root = high
    while root * root != x:
       root = (low + high) // 2
       if low + 1 >= high:
          return False
       if root * root > x:
          high = root
       else:
          low = root
    return True

Then you can run through the integers from 0 to 100 like this:

n = 0
while n <= 100:
    x = math.factorial(n) + 1
    if isSquare(x):
        print n
    n = n + 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

perfect square algorithm - explanation for the implementation

From Dev

Understanding the code for finding the permutation of numbers x and y,(less than n) such that x*y is a perfect square and y -x is maximum (x < y)

From Dev

Finding square centers from a picture

From Dev

How to make perfect square shaped image/button

From Dev

Finding a square in a group of coordinates

From Dev

Optimized way to find if a number is a perfect square

From Dev

Finding perimeter of a recursively modified square

From Dev

Finding square of a number

From Dev

Finding the numbers of square that can be formed

From Dev

perfect square including both end values

From Dev

How to check if an integer is a perfect square

From Dev

How to find the square root of perfect square?

From Dev

Get closest number to perfect square in batch

From Dev

JavaScript - Improving algorithm for finding square roots of perfect squares without Math.sqrt

From Dev

Finding the largest square in an n * n grid

From Dev

Finding prime number between 1 and N

From Dev

Complexity of checking whether a BigInteger is a perfect square

From Dev

Working on vectors and finding their square

From Dev

Define a function to check if its a perfect square in C

From Dev

Finding square centers from a picture

From Dev

Finding perimeter of a recursively modified square

From Dev

Finding the square of a number without multiplication

From Dev

perfect square including both end values

From Dev

Finding the largest square in an n * n grid

From Dev

JavaScript - Improving algorithm for finding square roots of perfect squares without Math.sqrt

From Dev

Perfect Square Root in PHP

From Dev

Finding the biggest square in a rectangle with a condition

From Dev

Recursively finding a sum of perfect squares in a list

From Dev

Why is this not drawn as an perfect square?

Related Related

  1. 1

    perfect square algorithm - explanation for the implementation

  2. 2

    Understanding the code for finding the permutation of numbers x and y,(less than n) such that x*y is a perfect square and y -x is maximum (x < y)

  3. 3

    Finding square centers from a picture

  4. 4

    How to make perfect square shaped image/button

  5. 5

    Finding a square in a group of coordinates

  6. 6

    Optimized way to find if a number is a perfect square

  7. 7

    Finding perimeter of a recursively modified square

  8. 8

    Finding square of a number

  9. 9

    Finding the numbers of square that can be formed

  10. 10

    perfect square including both end values

  11. 11

    How to check if an integer is a perfect square

  12. 12

    How to find the square root of perfect square?

  13. 13

    Get closest number to perfect square in batch

  14. 14

    JavaScript - Improving algorithm for finding square roots of perfect squares without Math.sqrt

  15. 15

    Finding the largest square in an n * n grid

  16. 16

    Finding prime number between 1 and N

  17. 17

    Complexity of checking whether a BigInteger is a perfect square

  18. 18

    Working on vectors and finding their square

  19. 19

    Define a function to check if its a perfect square in C

  20. 20

    Finding square centers from a picture

  21. 21

    Finding perimeter of a recursively modified square

  22. 22

    Finding the square of a number without multiplication

  23. 23

    perfect square including both end values

  24. 24

    Finding the largest square in an n * n grid

  25. 25

    JavaScript - Improving algorithm for finding square roots of perfect squares without Math.sqrt

  26. 26

    Perfect Square Root in PHP

  27. 27

    Finding the biggest square in a rectangle with a condition

  28. 28

    Recursively finding a sum of perfect squares in a list

  29. 29

    Why is this not drawn as an perfect square?

HotTag

Archive