Irrational Numbers – Python Challenge

An irrational number is a real number that cannot be expressed as a ratio of integers, in other words you cannot write an irrational number as a fraction p/q where p and q are both integer values.

Two of the most well known irrational numbers are Pi (Π) and the Golden ration (φ).

One of the characteristics of irrational numbers is that their decimal expansion is infinite with no recurring patterns.

Another interesting fact about irrational numbers is that the square root of a prime number is always irrational. So the following numbers are all irrational numbers:

We will base our Python challenge based on this latest fact. Your task is going to write a Python script that:

    Ask the user to enter two positive integer values, a and b.
    List a set of irrational numbers between a and b by listing the square roots of all the prime numbers between a2 and b2.

For instance for a = 4 and b = 7, we will find all the prime numbers between a2 = 16 and b2 = 49 and work out a list of irrational numbers using the square root values of these prime numbers. So √17, √19, √23, √29, √31, √37, √41, √43, √47 are all irrational numbers between 4 and 7. We will display a rounded version of these numbers on screen as it is impossible to display an irrational number with its full decimal expansion.

Note that to help you with this code, we have already created a function called isPrime() that takes a number as a parameter and returns True is this number is a prime number.

def isPrime(number):
   if number > 1:
      for i in range(2, int(number ** 0.5) + 1):
         if number % i == 0:
            return False
      return True
   else:
      return False

Also note that the square root of a number can be calculated by raising this number to the power of half.

Python Code

You can now complete this challenge using the code provided below:

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4.3 / 5. Vote count: 6

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!