Ramanujan Number or Taxicab Number in Java

In this section, we will learn what is Ramanujan number (or Hardy-Ramanujan number) and how to check whether the number is Ramanujan or not through a Java program.

Ramanujan Number

In mathematics, the Ramanujan number is a magical number. It can be defined as the smallest number which can be expressed as a sum of two positive integer cubes in n-distinct ways. It is also known as Taxicab number. It is denoted by Ta. The most popular taxicab number is 1729.

The number 1729 is related to a taxi. Once Ramanujan was admitted in the hospital and G.H. Hardy (British mathematician) arrived for a visit by taxi. The number of the taxi in which he traveled was 1729. He remarked that the number of my taxi seems a dull number.

After hearing the words of Hardy, Ramanujan replied, No Hardy! It is a very interesting number. Hardy asked, how? Then, Ramanujan replied, the number 1729 is the smallest number expressible as the sum of two cubes in two distinct ways. Since then it is known as the Ramanujan or Hardy-Ramanujan or Taxicab number. Let's check the Ramanujan statement.

1729= 103+93=1000+729
1729= 13+123=1+1728

Some other examples of Ramanujan numbers are:

4104= 23+163=8+4096
4104= 93+153=729+3375
13832= 243+23=13824+8
13832= 183+203=5832+8000

It is also possible that a taxicab number may expressible as the sum of two cubes in three distinct ways. For example, the number 15,170,835,645 is equals to:

5173+24683=7093+24563=17333+21523

It is also possible that a taxicab number may expressible as the sum of two cubes in four distinct ways. For example, the number 1,801,049,058,342,701,083 is equals to:

922273+12165003=1366353+12161023=3419953+12076023==6002593+11658843

It is difficult to determine the Ta(N) because the numbers get very large

Approaches to Find Ramanujan Number

There are two approaches to find the Ramanujan number. Let's understand each one by one with examples.

Approach 1:

A magical number that is equal to the product of the sum of all digits of a number and reverse of the sum. Let's follow the steps to find the Ramanujan number.

Step 1: Read an integer from the user.

Step 2: Sum up the individual digits.

Step 3: Find the reverse of the sum.

Step 4: Calculate the product of the original number and reversed number.

If the product is the same as the original number (that we have entered), the number is a Ramanujan number, else not. Let's understand it through an example.

Example

Suppose, we want to check the number 1458 is a Ramanujan number or not.

  1. 1458 = 1 + 4 + 5 + 8 =18
  2. 18 -> 81
  3. 18 * 81 = 1458

We get the same number that we have taken. Hence, the given number is a Ramanujan or taxicab number.

Let's implement the above approaches in a Java program.

Approach 2:

It this approach, the given number is expressible as the sum of two cubes in N different ways. For example:

1729= 103+93= 13+123

Ramanujan Number Program in Java

RamanujanNumberExample1.java

Output 1:

Enter a number you want to check: 1729
1729 is a Ramanujan Number.

Output 2:

Enter a number you want to check: 1428
1428 is not a Ramanujan Number.

Let's create another program that finds the Ramanujan number between specified range.

RamanujanNumberExample2.java

Output 1:

Ramanujan Number between 1000 to 1000000:
1458
1729

Output 2:

Ramanujan Number between 1 to 100000000:
1
81
1458
1729





Latest Courses