Friedman Number: How to Solve using Java?

5+

Well we already know much about the Friedman number at my sister blog (tellmehow.co). If you want to know about this number then check the given blog.

Let’s move to recipe of Friedman number

Friedman Number: How to Solve using Java?
Friedman Number: How to Solve using Java?

What is the concept behind the Friedman number?

This number is little bit complex in first time hearing. But today I am going to share the real concept/logic which we are using to find the given number is Friedman number or not.

Let’s take an example: 125 is a Friedman number or not.

Step 1: Find the permutation for given number

In the beginning, We have to find the permutation of given number . So in this case permutation of 125 would be [125, 215, 512]. So how to find the permutation of given string? This is the first hurdle of our logic.


Output of above program for number 125 would be:

[125, 521, 215, 512, 251, 152]

Step 2: Create expression for Friedman number and evalute them

In the next step we have to Iterate the Set data and find the expression and evaluate that.

Find the expression for friedman number and evaluate that
Find the expression for friedman number and evaluate that

Don’t worry about it this is the important logic part to find the Friedman number. In this step you have to create expression with given permutated number.

(expression1 symbol expression2) i.e (1+25), (12-5), (12^5) etc. and evaluate that expression. So what would we do next?

We are going to find the expression1 and expression 2 firstly. As we know that length of 125 is 3. So for the first expression, we are going to substring the given string from 0 to length-1. And for second expression substring the given data 1 to length.

Find the expression1 and expression2 for Friedman number
Find the expression1 and expression2 for Friedman number

From the above statement, expression1 would be from 12 i.e. 1, 2, 12, 21 and expression 2 would be from 25 i.e. 2, 5 , 25, 52.

For making a proper expression, we have to add symbols (+, -, *, /, ^) between the the above expression1 and expression2. For that we creating a array of symbol.

Step 3: Club and generate valid expression

Now club the all our discussion in Step 2.


At the line number 5 and 7, we find the expression1 and expression 2 respectively. Then another loop for symbol array at line number 9.

See the line 10, for the expression which generating by these three recursive loop. Now we have to find the generated expression is valid or not.

Step 4: Find expression is valid or not

Now we are going to find the generated expression is valid or not in Java.


Output:

(1+25)

If it return true then we will evaluate the given expression.

Step 5: Evaluate the generated expression

Find the output of given expression string in Java


Output

(1+25) – 26

Step 6: Finally check the given number and evaluate number is same or not

Now check the given string i.e 125 with the output of above evaluation of expression. If both are same then that number is friedman number and generated expression is correct for that number.

If you want full working source code for find the number is Friedman number or not then fork me at given link.

Fork me at Github

Welcome for new coder who can optimize the code and run for 10 digit. Are you accepting my challenge? Comment me what you think.

5+
Summary
Friedman Number: How to Solve using Java?
Article Name
Friedman Number: How to Solve using Java?
Description
What is the concept behind the Friedman number? This number is little bit complex in first time hearing. Find the given number is Friedman number or not.
Author
Publisher Name
Das JS