Followers

Showing posts with label Hardy-Ramanujan Number. Show all posts
Showing posts with label Hardy-Ramanujan Number. Show all posts

Wednesday, April 26, 2023

A Python program to check if a given number is a Hardy-Ramanujan number or not.

 

The Hardy-Ramanujan number is a number that can be expressed as the sum of two cubes in two different ways. In other words, a positive integer n is a Hardy-Ramanujan number if there exist six positive integers a, b, c, d, e, and f such that:

n = a^3 + b^3 = c^3 + d^3 = e^3 + f^3

The name "Hardy-Ramanujan" comes from the famous mathematicians G. H. Hardy and Srinivasa Ramanujan, who were both fascinated by the properties of these numbers.

The first few Hardy-Ramanujan numbers are:

1729, 4104, 13832, 20683, 32832, 39312, 40033, 46683, 64232, 65728, ...

The smallest of these, 1729, is often referred to as the "taxicab number" because it was supposedly the answer to a question posed by Hardy to Ramanujan while they were in a taxicab in London.

Here's a Python program to check if a given number is a Hardy-Ramanujan number or not:

python
number = int(input("Enter a number: ")) # Generate a list of all possible cube combinations cubes = [(i**3 + j**3, i, j) for i in range(1, number) for j in range(i, number)] # Check if the number appears in the list in two different combinations count = sum(1 for (k, i, j) in cubes if k == number) if count >= 2: print(number, "is a Hardy-Ramanujan number") else: print(number, "is not a Hardy-Ramanujan number")

In this program, I first take the input number from the user. I then generate a list of all possible combinations of two cubes less than the input number using a list comprehension. For each combination (i, j), I calculate the sum of their cubes and store it along with i and j in a tuple.

I then count the number of times the input number appears in the first element of the tuples using another list comprehension and the sum() function. If the count is greater than or equal to 2, I print a message saying that the number is a Hardy-Ramanujan number. Otherwise, I print a message saying that the number is not a Hardy-Ramanujan number.