×

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.

  1. 1458 = 1 + 4 + 5 + 8 =18
  2. 18 -> 81
  3. 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.

Related Topics

Java Tutorial

What is Java? Java is an object-oriented, robust, secured and platform-independent programming language. With the help of Java Programming, we can develop console, window, web, enterprise and mobile applications. Java language was...

22 minutes read.

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

Java Regular Expressions

Java Regular Expressions The Java Regex or Regular Expression is an API that defines a pattern for searching or manipulating strings. A regular expression is a pattern that can be as simple as...

8 minutes read.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

How to Convert Hexadecimal to Decimal in Java

How to Convert Hexadecimal to Decimal in Java There are two methods to convert Hexadecimal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method It is a static method of...

2 minutes read.

Java FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Java Strings

String class is the most used class in Java programming language. The string is the sequence of characters, which is treated as objects in Java. Creating String objects We create String objects...

5 minutes read.