Java help

Place to talk about all that new hardware and decaying software you have.

Moderator: General Mods

Post Reply
Starman Ghost
Trooper
Posts: 535
Joined: Wed Jul 28, 2004 3:26 am

Java help

Post by Starman Ghost »

I need help with a java assignment. Basically I have to inverse a number without converting it from int. It must remain an int. I've been able to do that with the forllowing ugly code:

Code: Select all

while(num > 0)
{
if (num / 10 < 0)
return(num);
else
{
	rem = num % 10;
	sum = sum * 10 + rem;
	num = num / 10;
}
}
return(sum);
And whatever other code I have for intializing variables and inputing the number. The problem is, I need to do this using a recursive method(as in the method calls itself). I can't fiugure out how to do this. Any help?
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]
Deathlike2
ZSNES Developer
ZSNES Developer
Posts: 6747
Joined: Tue Dec 28, 2004 6:47 am

Post by Deathlike2 »

Consider the following:

Here's some simple code to deal with adding numbers from 0 to X (assuming X is a positive num).

Code: Select all

int sum = 0;
while(num > 0)
{
  sum += num;
  num--;
}

return (sum);
Rewriting the same algo using recursion:

Code: Select all

if(num > 0)
  return(num + func_name(num-1);
else
  return(0);
See how the function calls itself, and decrements the value into its next iteration. Both do the same thing, just rewritten differently. I hope this gives you insight in the recursive code you are trying to write.
Continuing [url=http://slickproductions.org/forum/index.php?board=13.0]FF4[/url] Research...
Nach
ZSNES Developer
ZSNES Developer
Posts: 3904
Joined: Tue Jul 27, 2004 10:54 pm
Location: Solar powered park bench
Contact:

Post by Nach »

Code: Select all

int numrev(int num)
{
  if (num/10)
  {
    int msd; //Most signifigtant digit
    double msd_power, smsd_power; //Power of the most signifigant and second most signifigant digits

    msd_power = Math.floor(Math.log10((double)num));
    msd = num/(int)Math.pow(10.0, msd_power);
    num -= msd*(int)Math.pow(10.0, msd_power);
    smsd_power = Math.floor(Math.log10((double)num));

    return(numrev(num)*(int)Math.pow(10.0, msd_power-smsd_power) + msd);
  }
  return(num);
}
Tested with the following numbers:

Code: Select all

0 0
7 7
12 21
26 62
123 321
12345 54321
1240426 6240421
10 1
12045008 80054021
The real trick was handling the 0 in the middle correctly during the recursion.
May 9 2007 - NSRT 3.4, now with lots of hashing and even more accurate information! Go download it.
_____________
Insane Coding
Starman Ghost
Trooper
Posts: 535
Joined: Wed Jul 28, 2004 3:26 am

Post by Starman Ghost »

Thanks guys, I got it. Just for kicks here is the final recursive method

Code: Select all

	public static int inverseNum(int num, int sum, int rem)
	{
		if (num / 10 < 1)
		{
			if (rem == 0 || sum == 0)
			return(num);
			else
			return(num + (sum * 10));
		}
		else
		{
			rem = num % 10;
			sum = sum * 10 + rem;
			num = Math.round(num / 10);
			sum = inverseNum(num, sum, rem);
		}
		return(sum);
	}
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

Alternatively, the local voodoonist strongly advises you use the mojo of this sample:

Code: Select all

unsigned int revvup(unsigned int in, unsigned int out)
{
  return ((in) ? (revvup(in/10, (in%10)+out*10)) : out);
}
Don't need no steekeeng pows to do integer crap !

*cough*

This shameless plug sponsored by bat-a-cat, inc.
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
Nach
ZSNES Developer
ZSNES Developer
Posts: 3904
Joined: Tue Jul 27, 2004 10:54 pm
Location: Solar powered park bench
Contact:

Post by Nach »

Yep, grin's is the best.

Rewritten for Java and simplicity of use:

Code: Select all

int revvup(int in, int out = 0)
{
  return (in ? revvup(in/10, (in%10)+out*10) : out);
}
Tested fine with the above, plus:

Code: Select all

-4136 -6314
This is the one to use.

Starman Ghost:
Yours fails on negative numbers, and the Math.round() in this is also pointless. You should also be using default values for the extra parameters.
Last edited by Nach on Wed May 02, 2007 4:50 pm, edited 1 time in total.
May 9 2007 - NSRT 3.4, now with lots of hashing and even more accurate information! Go download it.
_____________
Insane Coding
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

grinvader wrote:Alternatively, the local voodoonist strongly advises you use the mojo of this sample:

Code: Select all

unsigned int revvup(unsigned int in, unsigned int out)
{
  return ((in) ? (revvup(in/10, (in%10)+out*10)) : out);
}
Don't need no steekeeng pows to do integer crap !

*cough*

This shameless plug sponsored by bat-a-cat, inc.
yeah, really.
Does [Kevin] Smith masturbate with steel wool too?

- Yes, but don’t change the subject.
Deathlike2
ZSNES Developer
ZSNES Developer
Posts: 6747
Joined: Tue Dec 28, 2004 6:47 am

Post by Deathlike2 »

funkyass wrote:
grinvader wrote:Alternatively, the local voodoonist strongly advises you use the mojo of this sample:

Code: Select all

unsigned int revvup(unsigned int in, unsigned int out)
{
  return ((in) ? (revvup(in/10, (in%10)+out*10)) : out);
}
Don't need no steekeeng pows to do integer crap !

*cough*

This shameless plug sponsored by bat-a-cat, inc.
yeah, really.
The problem though is that the people grading this will be like, "wtf, hax".
Continuing [url=http://slickproductions.org/forum/index.php?board=13.0]FF4[/url] Research...
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

Deathlike2 wrote:The problem though is that the people grading this will be like, "wtf, hax".

Code: Select all

 push edx
 push ecx
 push ebx
 mov ebx,10
 xor ecx,ecx
.dive
 cmp eax,0
 je .exit
 xor edx,edx
 div ebx
 lea ecx,[edx+10*ecx]
 jmp .dive
.exit
 mov eax,ecx
 pop ebx
 pop ecx
 pop edx
Wtf, hax ?
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
Deathlike2
ZSNES Developer
ZSNES Developer
Posts: 6747
Joined: Tue Dec 28, 2004 6:47 am

Post by Deathlike2 »

grinvader wrote:
Deathlike2 wrote:The problem though is that the people grading this will be like, "wtf, hax".

Code: Select all

 push edx
 push ecx
 push ebx
 mov ebx,10
 xor ecx,ecx
.dive
 cmp eax,0
 je .exit
 xor edx,edx
 div ebx
 lea ecx,[edx+10*ecx]
 jmp .dive
.exit
 mov eax,ecx
 pop ebx
 pop ecx
 pop edx
Wtf, hax ?
They would go "oh shit" and crap in pants.
Continuing [url=http://slickproductions.org/forum/index.php?board=13.0]FF4[/url] Research...
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

I wonder if taking the integer, and making in into an integer object counts as converting it.
Does [Kevin] Smith masturbate with steel wool too?

- Yes, but don’t change the subject.
Starman Ghost
Trooper
Posts: 535
Joined: Wed Jul 28, 2004 3:26 am

Post by Starman Ghost »

Deathlike2 wrote:
funkyass wrote:
grinvader wrote:Alternatively, the local voodoonist strongly advises you use the mojo of this sample:

Code: Select all

unsigned int revvup(unsigned int in, unsigned int out)
{
  return ((in) ? (revvup(in/10, (in%10)+out*10)) : out);
}
Don't need no steekeeng pows to do integer crap !

*cough*

This shameless plug sponsored by bat-a-cat, inc.
yeah, really.
The problem though is that the people grading this will be like, "wtf, hax".
Yeah really. It's pretty sweet, but kinda confusing.

Yeah, I didn't need the round function, so I removed it. I thought I was going to ned it but didn't. I also made it do negative numbers too. Was just a matter of and extra condition using my code.

Code: Select all

	public static int inverseNum(int num, int sum, int rem)
	{
		if (num / 10 < 1 && num /10 > -1)
		{
			if (rem == 0 || sum == 0)
			return(num);
			else
			return(num + (sum * 10));
		}
		else
		{
			rem = num % 10;
			sum = sum * 10 + rem;
			num = num / 10;
			sum = inverseNum(num, sum, rem);
		}
		return(sum);
	}
Not as clean as the code of others, but it works. If I make it too complex like grins they'll think I cheated or something. Anyways, yep.
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

just to make sure, the assignment is to print the number backwards, not express it as denominator.
Does [Kevin] Smith masturbate with steel wool too?

- Yes, but don’t change the subject.
Starman Ghost
Trooper
Posts: 535
Joined: Wed Jul 28, 2004 3:26 am

Post by Starman Ghost »

funkyass wrote:just to make sure, the assignment is to print the number backwards, not express it as denominator.
Yes, that is correct. Sorry if I wasn't clear. I noticed now that inverse can mean x inverse = 1/x. Oopsie.
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]
Deathlike2
ZSNES Developer
ZSNES Developer
Posts: 6747
Joined: Tue Dec 28, 2004 6:47 am

Post by Deathlike2 »

Starman Ghost wrote:
funkyass wrote:just to make sure, the assignment is to print the number backwards, not express it as denominator.
Yes, that is correct. Sorry if I wasn't clear. I noticed now that inverse can mean x inverse = 1/x. Oopsie.
You meant you wanted to "invert" the digits.
Continuing [url=http://slickproductions.org/forum/index.php?board=13.0]FF4[/url] Research...
Starman Ghost
Trooper
Posts: 535
Joined: Wed Jul 28, 2004 3:26 am

Post by Starman Ghost »

Deathlike2 wrote:
Starman Ghost wrote:
funkyass wrote:just to make sure, the assignment is to print the number backwards, not express it as denominator.
Yes, that is correct. Sorry if I wasn't clear. I noticed now that inverse can mean x inverse = 1/x. Oopsie.
You meant you wanted to "invert" the digits.
Yes, that's what I meant to say. I misspoke.
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]
Nach
ZSNES Developer
ZSNES Developer
Posts: 3904
Joined: Tue Jul 27, 2004 10:54 pm
Location: Solar powered park bench
Contact:

Post by Nach »

Um...

Code: Select all

if (num / 10 < 1 && num /10 > -1) 
Ever heard of equal to 0?

Code: Select all

if (num / 10 == 0) 
Since you're dealing with integers, there is no fraction.
May 9 2007 - NSRT 3.4, now with lots of hashing and even more accurate information! Go download it.
_____________
Insane Coding
Starman Ghost
Trooper
Posts: 535
Joined: Wed Jul 28, 2004 3:26 am

Post by Starman Ghost »

Nach wrote:Um...

Code: Select all

if (num / 10 < 1 && num /10 > -1) 
Ever heard of equal to 0?

Code: Select all

if (num / 10 == 0) 
Since you're dealing with integers, there is no fraction.
Haha. I amaze myself sometimes. I was so tired I forgot that int has no decimal places. I'll change that, thanks.
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]
Deathlike2
ZSNES Developer
ZSNES Developer
Posts: 6747
Joined: Tue Dec 28, 2004 6:47 am

Post by Deathlike2 »

Starman Ghost wrote:
Nach wrote:Um...

Code: Select all

if (num / 10 < 1 && num /10 > -1) 
Ever heard of equal to 0?

Code: Select all

if (num / 10 == 0) 
Since you're dealing with integers, there is no fraction.
Haha. I amaze myself sometimes. I was so tired I forgot that int has no decimal places. I'll change that, thanks.
More importantly, truncation is done instead.
Continuing [url=http://slickproductions.org/forum/index.php?board=13.0]FF4[/url] Research...
Post Reply