×

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 Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

nth Prime Number in Java

A number is considered prime if only divisible by one and itself. Prime numbers include, for example, 2, 3, 5, 7, 11, etc. In looking at it another way, a...

5 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

StringBuilder in Java

StringBuilder in Java Java StringBuilder class is introduced since JDK 1.5. The StringBuilder class is mainly used to create modifiable or mutable strings. Note that the StringBuilder class is not synchronized....

7 minutes read.

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program. Fall through It is a situation where there isn't a break statement in...

3 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Enterprise Java Beans

One of the many Java APIs for the common development of corporate software is Enterprise Java Beans (EJB). An EJB, a server-side software component, contains the business logic of an...

4 minutes read.

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.

Types of Events in Java

One of Java's key ideas is the principle of an event. Events in Java are activities that result in a change in the state or behavior of an object. The...

4 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

3 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

Activity selection problem in Java

The activity selection problem is a multiple objective problem hat requires choosing non-conflicting tasks to complete within a specific amount of time from a list of tasks identified by a...

5 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 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.