Ramanujan Number or Taxicab Number in Java
In this section, we will discuss what a Ramanujan number (also known as a Hardy-Ramanujan number) is and how to use a Java programme to determine if a given integer is a Ramanujan number or not.
Ramanujan number
The Ramanujan number is really a mysterious number in mathematics. It can be described as the smallest integer that can be combined in n different ways to give the sum of the two positive integer cubes. It also goes by the name "Taxicab number." Ta is the symbol for it. The most often used taxi number is 1729.
There is a cab connection to the number 1729. G.H. Hardy, a British mathematician, visited for a visit after Ramanujan was given a hospital admission. The taxi that he rode in had the number 1729. He said that my taxi's number seems like a boring number.
Ramanujan retorted, "No, Hardy," upon hearing Hardy's statements. It's a really fascinating number. How, Hardy questioned. The least number that can be expressed as that of the sum of the two cubes in a variety of ways, Ramanujan continued, is 1729. Since then, it has been referred to as the Hardy-Ramanujan, Ramanujan, or taxicab number. Let's verify Ramanujan's assertion.
2=1^3+1^3
9=2^3+1^3
Other instances of Ramanujan numbers include:
1729=12^3+13=103+93
4104=163+23=153+93
13832=243+23=203+183
20683=273+103=243+193
A taxicab number might alternatively be expressed as the total of two cubes in one of three different ways. For instance, 15,170,835,645 is equivalent to:
517^3+2468^3=709^3+2456^3=1733^3+2152^3
The Ta(N) is challenging to calculate since the numbers grow quite huge.
Methods to Discover the Ramanujan Number
The Ramanujan number can be discovered using either of two methods. Let's examine each in turn using examples.
Method 1:
The magical number which is the result of adding the total digits of such a number together and then subtracting that total. Let's carry out the procedures to discover the Ramanujan number.
Step-1: Read a user-provided integer in the first step.
Step 2: Add up each digit separately.
Step 3: Determine the sum's inverse.
Step 4: Compute the multiplication of the initial number with the inverted number in the step four.
A value is a Ramanujan number if the result is equivalent to the original number (what we entered), otherwise it is not. Let's provide an example to help you grasp it.
Example
Let's say we wish to determine whether or not number 1458 is now a Ramanujan number.
- 1458 = 1 + 4 + 5 + 8 =18
- 18 -> 81
- 18 * 81 = 1458
The result is the same as the one we entered. Therefore, the provided number is a taxicab or Ramanujan number.
Let's include the above methods into a Java program.
RamanujanNumberDemo1.java
import java.util.Scanner;
public class RamanujanNumberDemo1
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.print("Enter the number you want to check:");
// taking a user-provided integer and checking it
int origNum = sc.nextInt();
int sums = findtheSum(origNum);
int reverseSums = reverseSums(sums);
// compares the user-entered original number to the sum of the product of the original and reversed numbers
if( (sums * reverseSums) == origNum)
// if the preceding statement is accurate, prints the next one.
System.out.println(origNum +" is a Ramanujan Number.");
// if such condition returns false, publishes the next sentence; otherwise
System.out.println(orgNum+" is not a Ramanujan Number.");
}
// The sum is determined by a user-defined function.
public static int findTheSum(int numb)
{
// The variable holds the total of all the digits
int sums = 0;
while (numb > 0)
{
// determines the balance and incorporates the outcome into the variable sum
sums = sums + numb % 10;
// takes the number's last digit out.
numb = numb / 10;
}
return sums;
}
// a user-defined function that subtracts from the total shown by the findSum() function
public static int reverseTheSum(int numb)
{
// the variable keeps track of the opposite of the variable sum.
int reverse = 0;
// carries out operations till the condition is false
while (numb > 0)
{
// remainder is determined
int digits = numb % 10;
//sum Is reversed
reverse = reverse * 10 + digits;
// eliminates the last digit of the number.
numb = numb / 10;
}
return reverse;
}
}
Output:
Enter the number you want to check: 4104
4104 is a Ramanujan Number.