Sort a number's digits recursively in Java - java

I'm very new to programming, just learned it in university. I have a task where I have to solve this problem recursively in java (without using arrays, if, else, while, etc...)
So the task is to sort numbers from 13542 to 12345.
public static void main(String[] args) {
System.out.println(sort(13542));
}
public static long sort(long n) {
return n < 10
? n
: sort(n, 0);
}
public static long sort(long n1, long n2) {
return n1 > 10
? xxx
: xxx;
}
The problem is that I have no idea what to do. I think my start is okay, but I have problems with the second method.

Firstly, recursion means, put simply, that you have something call itself repeatedly. The fact that the assignment is on recursion is a hint of how your lecturer wants you to solve it, using a recursive method.
Ignoring the main for now, since while it could be prettied up and made more elegant, that isn't the core of the problem.
public int recursiveSort(int toSort){
}
And for neatness, we'll want a method to check if it is sorted, and to do the sorting.
public Boolean isSorted(int toCheck){
//TODO: Check if input is sorted
}
public int singleSort(int toSort){
//TODO: Sorting algorithm
}
Which gives us a recursive method of
public int recursiveSort(int toSort){
toSort = singleSort(toSort);
return isSorted(toSort) ? toSort : recursiveSort(toSort);
}
The sorting with the constraints imposed is the tricky part, and depends on exactly what you cannot use.
And of course, try to look at different sorting algorithms and consider how you would implement them in this case.

Here's a pure recursion with one function and one argument; without log, power, string conversion or loops. I'd say this is quite a difficult exercise in recursion even for more than a beginner. I hope this helps. Feel free to ask for any clarification. (Simplifications are also welcome.)
JavaScript code:
function main() {
console.log(sort(13542));
}
function sort(n) {
if (n < 10)
return n;
let r = n % 10;
let l = (n - r) / 10 % 10;
let sorted = sort(Math.floor(n / 10) - l + r);
let last = sorted % 10;
if (l < last)
return 10 * sort(sorted - last + l) + last;
else
return 10 * sorted + l;
}
main();

Every recursive method should include 2 main "ingredients":
A termination condition
A step forward
As you've mentioned, the obvious termination condition is that a number has only 1 digit, which means it's sorted (and therefore the recursion should stop).
The necessary progression of the method would be to remove a digit on every run, sort the smaller number and then merge the digits together.
As you can figure, the actual challenge can be either merging correctly, or separating efficiently.
I chose to locate the maximal digit, remove it from the original number and send the newly created number back into the recursive function. Eventually the method merges the sorted digits with the largest digit on their right.
public static void main(String[] args) {
System.out.println(sort(13542));
}
public static long sort(long n) {
// For testing purposes:
// System.out.println("sort(" + n + ")");
if (n < 10) return n; // Termination condition
int numOfDigits = (int)(Math.log10(n)+1);
long largestDigit = n % 10;
long restOfDigits = n / 10;
for(int i=0; i<numOfDigits; i++) {
long current = (long) (n / Math.pow(10, i)) % 10;
if (current > largestDigit) {
largestDigit = current;
restOfDigits = (long) Math.pow(10, i) * (n / (long) Math.pow(10, i + 1))
+ (n % (long) Math.pow(10, i));
}
}
// Merge the largest number on the right
return 10 * sort(restOfDigits) + largestDigit;
}
As you can see, for testing purposes it's best to check the recursive method on its beginning. You can either print or use a debugger to see its progression.

In it's simplest form, recursion is making a method call itself over and over. Here's a simple example.
public void eatAllFoodFromTable(Table tbl, Person prsn) {
if(tbl.hasFood()) {
prsn.sustain(1);
tbl.removeFood(1);
eatAllFoodFromTable(tbl, prsn); /*As you can see here,
the method calls itself. However, because the method has a condition
that can prevent it from running indefinitely (or a way to terminate),
it will repeat until the condition is met, then terminate. This is recursion!*/
} else {
//Do nothing.
}
}
What you want to do is take your long, and feed it into a method called sort, or similar. Then, that method will check to see if some of it is in order (through some kind of iteration), and then call itself (sort()) again with the new long generated from the sorting iteration.
Upon reaching the point where it is sorted, the method will terminate, returning the final sorted value.

Thanks alot for your help. I think I got it now:
public static long sort(long n) {
return n < 10
? n
: shuffle(sort(n / (long) Math.pow(10, count(n) / 2)),
sort(n % (long) (Math.pow(10, count(n) / 2))));
}
public static long count(long n) {
return n < 10
? 1
: 1 + count(n / 10);
}
public static long shuffle(long n1, long n2) {
return (n1 > 0 || n2 > 0)
? (n1 % 10 > n2 % 10)
? shuffle(n1 / 10, n2) * 10 + n1 % 10
: shuffle(n1, n2 / 10) * 10 + n2 % 10
: 0;
}
Sadly we weren't allowed to use if, else or while. This would have been so much easier. But thank you all :)

Related

Java - Recursive double factorial algorithm

could anybody help me to improve this algorithm? It's a recursive function which is basically doing fact(n) * fact(n) but I can't figure out how to make it more efficient.
static long doubleFactorial(int n)
{
if (n == 0)
return 1;
System.out.println("DoubleFactorial(" + n + ") called");
return n * doubleFactorial(n - 1) & doubleFactorial(n - 1);
}
Any help would be much appreciated!
Just save it to a variable and don't call it 2 times:
Here is your code(assuming you want to multiply the factorials and not AND them)
static long doubleFactorial(int n) {
if (n == 0)
return 1;
System.out.println("DoubleFactorial(" + n + ") called");
long fact=doubleFactorial(n - 1);
return n * fact * fact;
}
Also, it would be more efficient if you remove the recursion(but you may not want that):
static long doubleFactorial(int n){
long ret=1;
for(int i=2;i<n;i++){
ret=i*ret*ret;
}
}
This replaces the recursion with a loop what reduces the number of method calls and therefore improves the performance.
Also, it will not create a massive stack(and possibly StackOverflowErrors)
Your logic , doesn't return fact(n) * fact(n)
if you want so, You can define for example :
f(0)=1
f(1)=1
f(2)=2^2*f(1)=4*f(1)
.
.
f(10)=10^2*f(9)
.
.
f(n)=n^2*f(n-1)
So you can use the below code:
static long doubleFactorial(int n)
{
if (n == 0)
return 1;
System.out.println("DoubleFactorial(" + n + ") called");
return n * n * doubleFactorial(n - 1);
}
Try online doubleFactorial(5)=14400

recursive to iterative (java)

as a beginner in programming I am trying to convert the following recursive method to an iterative one but I just don't get the hang of it. The method or function has a binary tree like recursion and I would like to use an array for the iterative solution.. unfortunately I am very confused how to do it.
I have already checked the way of converting the fibonnaci recursive method to an iterative one. But I think this is not the same here. Also I am not sure if a tree search method is useful?! Any help, hint, idea would be appreciated. Thanks.
public static int funct(int n) {
if (n == 0) return 1;
if (n == 1) return 2;
if n > 1 return funct(n-2)*funct(n/2);
}
Since every n-th member is computed by others before if you can cache all in a list. You start by adding the first 2 known members. Fibonacci its easier because you always need only previous value.
private static int func(int n) {
List<Integer> values = new ArrayList<>(n+1);
values.add(1);
values.add(2);
for (int i = 2; i <= n; i++) {
values.add(values.get(i - 2) * values.get(i / 2));
}
return values.get(n);
}
Now the real function is without last if:
public static int funct(int n) {
if (n == 0) return 1;
if (n == 1) return 2;
return funct(n-2) * funct(n/2);
}
As the recursive calls refer to smaller parameters one can cache all return values upto n.
Unfortunately this already spoils the pleasure, as the resulting code is complete:
public static int funct(int n) {
int[] results = new int[n+1];
results[0] = 1;
results[1] = 2;
int i = 2;
while (i <= n) {
results[i] = results[i-2] * results[i/2];
++i;
}
return results[n];
}
It indeed looks like fibonacci.
In general one would not need to fill all items of results. like probably results[n - 1].
Unfortunately you should have learnt prior to this problem:
Solving tail recursion.
Using a stack (like here) to use inner results of a recurive call.
You might look into those topics.
Math afterlude
The initial values are powers of 2. As the result is a product of earlier results, all results will be powers of 2.
f(0) = 1 = 2^0
f(1) = 2 = 2^1
f(n) = f(n - 2) * f(n / 2)
Hence you can introduce:
g(0) = 0
g(1) = 1
g(n) = g(n - 2) + g(n / 2)
f(n) = 2^g(n)
This will enlarge the range you can calculate as say 2100.
You will also see:
g(2k + 1) = g(2k) + 1
So you will only need a domain of even numbers:
g(2k) = g(2(k-1)) + g(k - k%2) + k%2

Possible multiplications of k distinct factors with largest possible factor n

Let M(n,k) be the sum of all possible multiplications of k distinct factors with largest possible factor n, where order is irrelevant.
For example, M(5,3) = 225 , because:
1*2*3 = 6
1*2*4 = 8
1*2*5 = 10
1*3*4 = 12
1*3*5 = 15
1*4*5 = 20
2*3*4 = 24
2*3*5 = 30
2*4*5 = 40
3*4*5 = 60
6+8+10+12+15+20+24+30+40+60 = 225.
One can easily notice that there are C(n,k) such multiplications, corresponding to the number of ways one can pick k objects out of n possible objects. In the example above, C(5,3) = 10 and there really are 10 such multiplications, stated above.
The question can also be visualized as possible n-sized sets containing exactly k 0's, where each cell that does not contain 0 inside it, has the value of its index+1 inside it. For example, one possible such set is {0,2,3,0,5}. From here on, one needs to multiply the values in the set that are different than 0.
My approach is a recursive algorithm. Similiarly to the above definition of
M(n,k), I define M(n,j,k) to be the sum of all possible multiplications of exactly k distinct factors with largest possible factor n, AND SMALLEST possible factor j. Hence, my approach would yield the desired value if ran on
M(n,1,k). So I start my recursion on M(n,1,k), with the following code, written in Java:
public static long M (long n, long j, long k)
{
if (k==1)
return usefulFunctions.sum(j, n);
for (long i=j;i<=n-k+1+1;i++)
return i*M(n,i+1,k-1);
}
Explanation to the code:
Starting with, for example, n=5 , j=1, k=3, the algorithm will continue to run as long as we need more factors, (k>=1), and it is made sure to run only distinct factors thanks to the for loop, which increases the minimal possible value j as more factors are added. The loop runs and decreases the number of needed factors as they are 'added', which is achieved through applying
M(n,j+1,k-1). The j+1 assures that the factors will be distinct because the minimal value of the factor increases, and k-1 symbolizes that we need 1 less factor to add.
The function 'sum(j,n)' returns the value of the sum of all numbers starting from j untill n, so sum(1,10)=55. This is done with a proper, elegant and simple mathematical formula, with no loops: sum(j,n) = (n+1)*n/2 - (i-1)*i/2
public static long sum (long i, long n)
{
final long s1 = n * (n + 1) / 2;
final long s2 = i * (i - 1) / 2;
return s1 - s2 ;
}
The reason to apply this sum when k=1, I will explain with an example:
Say we have started with 1*2. Now we need a third factor, which can be either of 3,4,5. Because all multiplications: 1*2*3, 1*2*4, 1*2*5 are valid, we can return 1*2*(3+4+5) = 1*2*sum(3,5) = 24.
Similiar logic explains the coefficient "i" next to the M(n,j+1,k-1).
say we have now the sole factor 2. Hence we need 2 more factors, so we multiply 2 by the next itterations, which should result in:
2*(3*sum(4,5) + 4*sum(5,5))
However, for a reason I can't explain yet, the code doesn't work. It returns wrong values and also has "return" issues that cause the function not to return anything, don't know why.
This is the reason i'm posting this question here, in hope someone will aid me. Either by fixing this code or sharing a code of his own. Explaining where I'm going wrong will be most appreciable.
Thanks a lot in advance, and sorry for this very long question,
Matan.
-----------------------EDIT------------------------
Below is my fixed code, which solves this question. Posting it incase one should ever need it :) Have fun !
public static long M(long n, long j, long k)
{
if (k == 0)
return 0;
if (k == 1)
return sum(j,n);
else
{
long summation = 0;
for (long i=j; i<=n; i++)
summation += i*M(n, i+1, k-1);
return summation;
}
}
I see that u got ur answer and i really like ur algorithm but i cant control myself posting a better algorithm . here is the idea
M(n,k) = coefficient of x^k in (1+x)(1+2*x)(1+3*x)...(1+n*x)
u can solve above expression by divide and conquer algorithm Click Here to find how to multiply above expression and get polynomial function in the form of ax^n + bx^(n-1)....+c
overall algorithm time complexity is O(n * log^2 n)
and best part of above algorithm is
in the attempt of finding solution for M(n,k) , u will find solution for M(n,x) where 1<=x<=n
i hope it will be useful to know :)
I am not sure about your algorithm, but you certainly messed up with your sum function. The problem you have is connected to type casting and division of integer numbers. Try something like this:
public static long sum (long i, long n)
{
final long s1 = n * (n + 1) / 2;
final long s2 = (i * i - i) / 2;
return s1 - s2 ;
}
You have a problem with your sum function : here is the correct formula:
public static long sum (long i, long n)
{
double s1 = n*(n+1)/2;
double s2 = i*(i-1)/2;
return (long)(s1-s2);
}
Here the full solution :
static int n = 5;
static long k = 3;
// no need to add n and k them inside your M function cause they are fixed.
public static long M (long start) // start = 1
{
if(start > k) // if start is superior to k : like your example going from 1..3 , then you return 0
return 0;
int res = 0; // res of your function
for(long i=start+1;i<n;i++){
res+=start*i*sum(i+1,n); // here you take for example 1*2*sum(3,5) + 1*3*sum(4,5).... ect
}
return res+M(start+1); // return res and start again from start+1 wich would be 2.
}
public static long sum (long i, long n)
{
if(i>n)
return 0;
double s1 = n*(n+1)/2;
double s2 = i*(i-1)/2;
return (long)(s1-s2);
}
public static void main(String[] args) {
System.out.println(M(1));
}
Hope it helped

Tried Euler's #14 again with recursion, didn't work for me. SPOILERS EULER 14th

I posted earlier trying to bruteforce it, but it didn't work. Here's my attempt #2 with recursion (first time using recursive methods). Please help!
Here's what happens: The code runs fine, with smaller numbers, but when we get up to one million, the code simply will run, and nothint at all happens. In Eclipse, it still gives me the option to end, but I've let it run for a very long time with nothing helping.
/**
* The following iterative sequence is defined for the set of positive
* integers:
*
* n → n/2 (n is even) n → 3n + 1 (n is odd)
*
* Using the rule above and starting with 13, we generate the following
* sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
*
* It can be seen that this sequence (starting at 13 and finishing at 1)
* contains 10 terms. Although it has not been proved yet (Collatz Problem),
* it is thought that all starting numbers finish at 1.
*
* Which starting number, under one million, produces the longest chain?
*
* NOTE: Once the chain starts the terms are allowed to go above one
* million.
*/
public class Euler14 {
static int desiredMax = 1000000;
static int maxTerm = 0;
static int maxNumberOfTerms = 0;
static int currentNumber = 0;
static int numberOfTerms = 0;
public static void doMath(int startingNumber) {
if(startingNumber == 1) {
System.out.print( maxTerm + " " + maxNumberOfTerms);
}
else {
currentNumber = desiredMax;
while(currentNumber!= 1) {
if(currentNumber%2 == 0) {
currentNumber = currentNumber/2;
numberOfTerms++;
} else {
currentNumber = (3 * currentNumber) + 1;
numberOfTerms++;
}
}
numberOfTerms++;
if(numberOfTerms > maxNumberOfTerms) {
maxNumberOfTerms = numberOfTerms;
maxTerm = startingNumber;
}
desiredMax--;
doMath(desiredMax);
}
}
public static void main(String[] args) {
doMath(desiredMax);
}
}
There are many wrong things with your code :
use of a recursive method which is no more no less than a loop going downward
use of static variables
numberOfTerms never reinitialized
as pointed by azurefrog, you have an integer overflow which is causing an infinite loop.
I was rearranging your code with as few changes as possible when he came up with the answer, so all I can do now is to show you a working code very similar to yours. See how cleaner it is this way :
public class Euler14 {
public static void main(String[] args) {
int maxTerm = 1000000;
int maxNumberOfTerms = 1;
// this loop replaces your recursion, which is not needed here and quite costly even if it is tail-recursion
for (int i = maxTerm ; i >= 2; i--) {
int numberOfTerms = 0;
// declare as long to prevent the overflow
long currentNumber = i;
while (currentNumber != 1) {
if (currentNumber % 2 == 0)
currentNumber = currentNumber / 2;
else
currentNumber = (3 * currentNumber) + 1;
numberOfTerms++;
if (numberOfTerms > maxNumberOfTerms) {
maxNumberOfTerms = numberOfTerms;
maxTerm = i;
}
}
}
System.out.println(maxTerm);
}
}
The main problem is that you are trying to do math on large numbers with ints. When your program gets down to a desiredMax of 999167, you're going into an infinite loop.
In Java, the largest value an int can represent is 2,147,483,647.
When your algorithm gets to 999167, it quickly exceeds that limit.
If you print the value of currentNumber in your inner while-loop, you see this:
...
1330496806
665248403
1995745210
997872605
-1301349480 <-- oops
-650674740
-325337370
...
You are trying to set currentNumber to 2,993,617,816, so your value is going to overflow.
This causes your while-loop to never terminate, since you don't account for negative numbers. You quickly settle into a repeating sequence of
-25
-74
-37
-110
-55
-164
-82
-41
-122
-61
-182
-91
-272
-136
-68
-34
-17
-50
-25
... ad infinitum
You could try switching to a bigger numerical representation (long), but, even if you switch to using long values, the way you are trying to recurse will cause a stack overflow long before you ever finish trying to evaluate a desiredMax of 1000000. (On my box, I get a StackOverflowError when I get down to 997474).
You need to go back and rethink the structure of your program. Recursion can be a useful tool, but it's dangerous to use unless you know that you aren't going to go too deep.
This is a good example of where you can employ Memoization.
Below is a solution that uses recursion, but avoids the need to continually go over paths you've calculated already.
This also separates the chain-calculation code from the searching-for-the-maximum code.
public class Euler14 {
static long[] records = new long[1000000];
// //////////////////////////////////////////////
// Recursively calculates one chain length
//
static long getLength(long n) {
// Terminating condition
if (n == 1) {
return n;
}
// Have we already calculated this?
if ((n < records.length) && (records[(int) n] != 0)) {
return records[(int) n];
}
// Haven't calculated this yet, so calculate it now
long length = getLength(n % 2 == 0 ? n / 2 : 3 * n + 1) + 1;
// Record the result for later use
if (n < records.length) {
records[(int) n] = length;
}
return length;
}
static long calculateQuestionFourteen() {
long maxLength = 0;
long maxStart = 0;
for (long i = 1; i < 1000000; ++i) {
long thisLength = getLength(i);
if (thisLength > maxLength) {
maxLength = thisLength;
maxStart = i;
}
}
return maxStart;
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
System.out.println(calculateQuestionFourteen());
System.out.println(System.currentTimeMillis() - start);
}
}

Round long from 1004L to 1000L (or 1006L to 1010L)

Suppose I have Long someLong = 1004L. What efficient method can I use to round this down to 1000L? Note that I do not actually know that someLong == 1004L so I can't simply do someLong -= 4L;. I need a generalizable method. I also want the ability to round down to each 5 instead of each 10, for example a function to round to 1005L (since if we're rounding by 5's then it'll round up instead of down).
More examples .. It could be that I have 1926L and I want to round to 5 meaning I need 1925L. Or I need to round to 10 meaning I need 1930L.
This is very simple.
If you want to round always down:
Your required formula is:
someLong-someLong%10
It is because someLong%10 is the remainder of someLong divided by 10. If you get this from the original number, you get the downrounded value, which you wanted.
The generalization is also simple: you can use 100, or even 13, if you want.
If you want to rounding in another direction (for example, rounding always up or always to the middle), then first to add something to this number, and then round always down.
If you want to round always up:
Then first you need to first add 9, then round always down.
someLong+9-(someLong+9)%10
If you want to round always to the middle:
...also you want to round to the nearest neightbor. Then you first add the half of the required interval, then round always down. For example, for 10 it is:
someLong+5-(someLong+5)%10
If you want to round a value towards the nearest multiple of step using the semantics of BigDecimal.ROUND_HALF_UP (if exactly halfway between two steps, round up), the necessary calculations are:
val += step/2;
val -= val%step;
Try this:
double a=1002l;
double b=a/10;
a=Math.round(b)*10;
System.out.println("Double round of value : "+a);
A generic function to round to the nearest multiple of k would be (works for positives only):
public static long round(long toRound, long k) {
long times = toRound / k;
long reminder = toRound % k;
if (reminder < k / 2) {
return k * times;
} else {
return k * (times + 1);
}
}
And a branchless variant (reminder < k / 2 => (2 * reminder / k) < 1:
public static long round(long toRound, long k) {
long times = toRound / k;
long reminder = toRound % k;
return k * (times + ((2 * reminder) / k));
}
The following example reachs what you need:
public static void main(String[] args) {
Long n = 1004L;
Long n2 = 1005L;
n = round(n);
n2 = round(n2);
System.out.println(n);
System.out.println(n2);
}
private static Long round(Long n) {
if (n%10 <=4) {
return n -=n%10;
} else {
return n += (10-n%10);
}
}
myFloor(long n, int m) {
return n - (n % m);
}
myRound(long n, int m) {
int i = (n % m) >= (m / 2) ? m : 0;
return n + i - (n % m);
}
so m could be 10 , 5 , ...

Categories