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.
- Convert a number (let's suppose the Number is num) to a string.
- 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.
- 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.
- 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.