×

Magnanimous Number in java

Magnanimous Number

When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left and right sides can have any magnitude, but they can never be zero.

Magnanimous Number Example:

Take 359794 as an example. The following table lists both of its halves, left and right.

Left part

Right part

3

59794

35

9794

359

794

3597

94

35979

4

Currently, the user verifies that the sum of the left and right parts is correct.

3 + 59794 = 59797

35 + 9794 = 9829

359 + 794 = 1153

3597 + 94 = 3691

35979 + 4 = 35983

Here, Five of the following are prime numbers: 35983, 59797, 9829, 1153, and 3691. Consequently, the number 359794 is a Magnanimous number.

Take 200 once more. The left and right halves of the number 100 are:

Left Part

Right Part

2

00

20

0

The left and right sections added together are now:

2 + 00 = 2

20 + 0 = 20

Thus, we have two numbers, one of which is two and the other 20, and neither is a prime number. As a result, at least one Number that is not a prime number was obtained. Thus, 200 is not a particularly impressive figure.

A Magnanimous Number's Finding Process:

The procedures for locating the magnanimous Number are listed below.

  1. Convert a number (let's suppose the Number is num) to a string.
  2.  To get the entire left and right portions of the string, traverse the string. The string must be translated into integers for each left and appropriate component.
  3. When the left and right halves are combined, the sum is created.Find the prime numbers in each generated sum (located in step 4) by checking them.
  4. The integer num is the grand Number if every sum formed is a prime number; otherwise, it is not.

A program based on Magnanimous Number in java:

MagnanimousNumberExample. java

// A Java application that tests for
// magnanimous numbers between 20 and 30 (20 and 30 inclusive)
public class MagnanimousNumberExample 
{ 
// how to determine whether or not it is prime
public boolean isPrime(int no) 
{ 
// dealing with the base case
// In addition to 1 and 0, negative numbers are also not prime.
if (no <= 1) 
{ 
return false; 
} 
// The prime numbers 2 and 3
if (no <= 3) 
{ 
return true; 
} 
// There are no prime numbers if the Number can be divided by 2 and 3.
if (no % 3 == 0 || no % 2 == 0) 
{ 
return false; 
} 
// Until number 4, it has been covered
// Consequently, the user can begin with the number 5.
// Any numeric handling above that is divisible by 2 and 3
// Consequently, the user can able to jump by 6
for(int j = 5; j * j <= no; j = j + 6) 
{ 
if (no % j == 0 || no % (j + 2) == 0) 
{ 
return false; 
} 
} 
return true; 
} 
// A technique to determine whether a number is magnanimous or not
public boolean isMagnanimous(int num) 
{ 
// The Number is changed into a string.
String str = Integer.toString(num); 
// determining the string's length
int len = str.length(); 
// A one-digit number is never capable of being a magnanimous number.
if (len < 2) 
{ 
return false; 
} 
// Finding every left and right portion of the string str using a loop
for(int j = 0; j < len - 1; j++) 
{ 
// left part 
String l = str.substring(0, j + 1); 
// right part 
String r = str.substring(j + 1); 
// the left half is converted to a string.
int intLeft = Integer.valueOf(l); 
// the right half is converted to a string.
int intRight = Integer.valueOf(r); 
// Adding the left and right halves together requires a prime number.
if (!isPrime(intLeft + intRight)) 
{ 
return false; 
} 
} 
return true; 
} 
// Main code 
public static void main(String argvs[]) 
{ 
// constructing an object of the MagnanimousNumberExample class
MagnanimousNumberExample obj = new MagnanimousNumberExample(); 
for(int i = 20; i <= 30; i++) 
{ 
Boolean flag = obj.isMagnanimous(i); 
if(flag) 
{ 
System.out.println(i + " is the magnanimous number."); 
} 
else 
{ 
System.out.println(i + " is not the magnanimous number."); 
} 
} 
} 
} 

Output:

20 is the magnanimous number.
21 is the magnanimous Number.
22 is not the magnanimous Number.
23 is the magnanimous Number.
24 is not the magnanimous Number.
25 is the magnanimous Number.
26 is not an magnanimous Number.
27 is not an magnanimous Number.
28 is not an magnanimous Number.
29 is an magnanimous Number.
30 is an magnanimous Number.

Related Topics

Catalan number in Java

In general mathematics, Catalan numbers can be defined as the sequence of natural numbers that frequently occur in counting problems often encountered in recursively defined objects. Mathematical formula of Catalan number Coming...

3 minutes read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

PriorityBlockingQueue Class in Java

What is the Queue? An abstract data structure like Stacks is a queue. A queue is open on both ends. Data is always pushed to one end, called enqueue, and removed...

4 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true...

2 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.

Java Boolean compareTo() method

The compareTo() method of Java Boolean class compares the Boolean argument with the Boolean instance and returns integer value, zero, or negative 1, or positive 1 based on the result...

2 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.

Insertion Sort in Java

Insertion Sort in Java Insertion sort in Java is a simple sorting algorithm that works in the same way as we hold cards in hand. Insertion sort does the sorting element-by-element,...

3 minutes read.

How to Convert String to enum in Java?

In this article, we shall gain the complete knowledge about how to convert the string to enum in Java. The complete process that happens in the approach shall be discussed...

3 minutes read.

PriorityQueue in Java

PriorityQueue in Java A PriorityQueue is a member of the Java Collection Framework and is used when the values are needed to be processed based on priority. Priority queue operates similar to...

8 minutes read.

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

flour pack problem in Java

Create a method called canPack and give it three int parameters: bigCount, smallCount, and objective. The bigCount option indicates the number of large flour bags (5 kilos each). The smallCount argument indicates...

3 minutes read.

Prime Number Program in Java Using a Scanner

In Java, a prime number is one that can only be divided by one or by itself and is greater than one. In other words, only one or itself can...

3 minutes read.