Jump to content

I need to write a bit of recursive code that outputs the cantor expansion of a base 10 number. The cantor expansion is as follows: x = an n! + an-1(n-1)!+...+a11! 
I'm not really sure where to start with this. I got a semi-working bit of code that does this with loops, but I need it done recursively. Any help is appreciated on starting this.

 

I understand basic recursion. A major problem I'm having is setting up logic for a cantor expansion using recursion though.
 
Loop code (doesn't display pieces of expansion with leading zeros, but needs to).

public class Test{	public static void main(String[] args)	{		int givenInt = 256;				for (int i = 0; i < 3; i++)		{			String built = stringBuild(givenInt);			System.out.println(built);			String check = "" + built.charAt(2);			givenInt = givenInt % computeFactorial(Integer.parseInt(check)); // remainder		}	}		public static String stringBuild(int givenInt)	{		int n = eachNumber(givenInt);		int multiple = givenInt / computeFactorial(n);				return multiple + "*" + n + "!";	}		public static int eachNumber(int givenInt)	{		int n = 1;		int result = computeFactorial(n);		while(result < givenInt)		{		   	n++;			   	result = computeFactorial(n);		}		n--;		return n;	}		public static int computeFactorial(int n)	{		int result = 1;		for (int i = 1; i <= n; i++) 		{			   result = result * i;		}				return result;	}}
Link to comment
https://linustechtips.com/topic/250309-help-with-java/
Share on other sites

Link to post
Share on other sites

recursive just means that you call yourself over again. so the do this recursively you would set up a base case (ie 1) and only return back then. otherwise you would "return" the result of the call of the same method with different parameter. so your first call would have 256 coming in. then the next one would be 255. then 254....until you get to 1 at which point you would simply return whatever you need to add on to the string.

Link to comment
https://linustechtips.com/topic/250309-help-with-java/#findComment-3432106
Share on other sites

Link to post
Share on other sites

recursive just means that you call yourself over again. so the do this recursively you would set up a base case (ie 1) and only return back then. otherwise you would "return" the result of the call of the same method with different parameter. so your first call would have 256 coming in. then the next one would be 255. then 254....until you get to 1 at which point you would simply return whatever you need to add on to the string.

I think you misunderstand. I need to output a cantor expansion. The cantor expansion for 256 = 2*5! + 0*4! + 2*3! + 2*2! + 0*1! , where ! is factorial.

 

As far as I understand, I need out find the largest factorial that is lower than the input, find out how many times it is used, and continuously do that with the remainder till there is none.

Link to comment
https://linustechtips.com/topic/250309-help-with-java/#findComment-3432125
Share on other sites

Link to post
Share on other sites

I think you misunderstand. I need to output a cantor expansion. The cantor expansion for 256 = 2*5! + 0*4! + 2*3! + 2*2! + 0*1! , where ! is factorial.

 

As far as I understand, I need out find the largest factorial that is lower than the input, find out how many times it is used, and continuously do that with the remainder till there is none.

i think you misunderstood.

 

Do you understand the basics of recursion? Traditional Fibonacci example.

as madknight has said now, do you understand recursion. that was what i was (trying) to explain. this seems easy to do recursively. to do factorial recursively is ridiculously easy. but requires you to set up the base case.

Link to comment
https://linustechtips.com/topic/250309-help-with-java/#findComment-3432166
Share on other sites

Link to post
Share on other sites

running your code i dont see anything for output 0*4! or 0*1!, are those values to be forgotten? or are they required in the final output

The small blurb above states it doesn't show pieces that have leading zeroes. They need to be shown, but I figured debugging code that I'm not going to actually use is pointless. 

Also, as I said, I understand basic recursion, but it's been quite a while since I've touched Java at all, and am having to for a discrete structures class. Am I missing some glaringly obvious solution?

Link to comment
https://linustechtips.com/topic/250309-help-with-java/#findComment-3432216
Share on other sites

Link to post
Share on other sites

	//recursive factorial	public static int computeFactorial(int n)	{		if(n == 1)		{			return n;		}		else		{			return n * computeFactorial(n - 1);		}	}

The small blurb above states it doesn't show pieces that have leading zeroes. They need to be shown, but I figured debugging code that I'm not going to actually use is pointless. 

Also, as I said, I understand basic recursion, but it's been quite a while since I've touched Java at all, and am having to for a discrete structures class. Am I missing some glaringly obvious solution?

the above code, that will recursively calculate a factorial. also what do each of these methods do? they may seem obvious to you but code comments are nice for others to try and follow along and help

Link to comment
https://linustechtips.com/topic/250309-help-with-java/#findComment-3432224
Share on other sites

Link to post
Share on other sites

	//recursive factorial	public static int computeFactorial(int n)	{		if(n == 1)		{			return n;		}		else		{			return n * computeFactorial(n - 1);		}	}

the above code, that will recursively calculate a factorial. also what do each of these methods do? they may seem obvious to you but code comments are nice for others to try and follow along and help

 

Each method just broke down a piece of what I assumed the recursion would take care of into separate pieces. eachNumber figures out the number of each factorial, based it fitting in the base(givenInt). Stringbuild just put them into a string including asterisk and exclamation point for final output. 

 

For clarity, that bit of code was to help me figure out what I was doing, and none of it will be in my final product.

Link to comment
https://linustechtips.com/topic/250309-help-with-java/#findComment-3432253
Share on other sites

Link to post
Share on other sites

Each method just broke down a piece of what I assumed the recursion would take care of into separate pieces. eachNumber figures out the number of each factorial, based it fitting in the base(givenInt). Stringbuild just put them into a string including asterisk and exclamation point for final output. 

 

For clarity, that bit of code was to help me figure out what I was doing, and none of it will be in my final product.

public class prog{		public static void main(String[] args)	{		int givenInt = 256;		//		for (int i = 0; i < 3; i++)//		{//			String built = stringBuild(givenInt);//			System.out.println(built);//			String check = "" + built.charAt(2);//			givenInt = givenInt % computeFactorial(Integer.parseInt(check)); // remainder//		}				System.out.println(contorExpansion(givenInt));	}		/**	 * Creates pretty string for printing	 * @param multiple number of times value can be used in expression	 * @param coefficent factorial coefficient	 * @return printable string	 */	public static String stringBuild(int multiple, int coefficent)	{				return String.valueOf(multiple) + "*" + String.valueOf(coefficent) + "!";	}		/**	 * Calculates highest coefficient	 * @param num number to not be larger than	 * @return highest coefficient whos factorial is less than num	 */	public static int calculateMaxCoefficient(int num)	{		//this is your code, so you should understand it				int n = 1; //why is this set to 1		int result = computeFactorial(n); //but then you compute the factorial of it...this would equal 1		while(result < num)		{		   	n++;			   	result = computeFactorial(n);		}		n--;		return n; 	}		/**	 * Computes factorial	 * @param n	number to be calculated	 * @return factorial	 */	public static int computeFactorial(int n)	{		if(n == 1)		{			return n;		}		else		{			return n * computeFactorial(n - 1);		}	}		public static String contorExpansion(int num)	{		if(num == 0) //recursive base case		{			return ""; //return empty string		}		else //do some math		{			int coefficient = calculateMaxCoefficient(num); //get the max coefficient for the factorial			int nextNum = 0; //next number to be used in recursive call			String ret = ""; //return value initialized			boolean useDefault = true; //just a boolean flag for if we can use max number (2) times the factorial value						for(int i=0; i<3;i++) //loop through possible multiplication values			{				if((i*computeFactorial(coefficient) > num)) //see if we pass the max number				{					useDefault = false; //if we do, then we do not want to use the default output					ret = stringBuild(i - 1, coefficient); //build that printable string					nextNum = num - ((i-1) * computeFactorial(coefficient)); //calculate the next number				}			}						if(useDefault) //if we didnt find a value less than 2 that was greater than the max number			{				ret = stringBuild(2, coefficient); //build a printable string				nextNum = num - (2 * computeFactorial(coefficient)); //calculate next number			}						return ret + "+" + contorExpansion(nextNum); //recursive call					}	}}

thats where i got to, it does use recursion in several locations hope it helps. it does not include for if the the number of times it goes in is 0. you could modify the last method there to have another parameter of max coefficient and then if the calcualted one is not one less than the one passed in, create a "0+n!" string and add it to ret

Link to comment
https://linustechtips.com/topic/250309-help-with-java/#findComment-3432519
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×