Originally posted by: damiano
what do you mean reverse the digits?
like 12345 becomes 54321 ?
Originally posted by: agnitrate
As always recursion is the answer.
[edit] I read digits as strings for some reason. You could modify the above method if you desired, but there's probably a better way for just integers, although I would still probably use recursion. [/edit]
-silver
Originally posted by: MCrusty
I need to take a number and reverse the digits of it. But since I am using Java and Strings are immutable it is very inefficient to use a String to reverse the digits, is there some sort of algebraic way to do this...or am I just out of luck?
Originally posted by: FeathersMcGraw
Originally posted by: MCrusty
I need to take a number and reverse the digits of it. But since I am using Java and Strings are immutable it is very inefficient to use a String to reverse the digits, is there some sort of algebraic way to do this...or am I just out of luck?
java.lang.StringBuffer is your friend.
Originally posted by: MCrusty
Originally posted by: agnitrate
As always recursion is the answer.
[edit] I read digits as strings for some reason. You could modify the above method if you desired, but there's probably a better way for just integers, although I would still probably use recursion. [/edit]
-silver
I am trying to stay away from strings, heh.
Originally posted by: damiano
Originally posted by: FeathersMcGraw
Originally posted by: MCrusty
I need to take a number and reverse the digits of it. But since I am using Java and Strings are immutable it is very inefficient to use a String to reverse the digits, is there some sort of algebraic way to do this...or am I just out of luck?
java.lang.StringBuffer is your friend.
or the divide and mod method cited above would work
Originally posted by: agnitrate
As always recursion is the answer.
Originally posted by: MCrusty
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.
Originally posted by: damiano
Originally posted by: MCrusty
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.
look above the mod and add method in a loop will be faster
Originally posted by: MCrusty
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.
Originally posted by: MCrusty
Originally posted by: damiano
Originally posted by: MCrusty
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.
look above the mod and add method in a loop will be faster
Yeah, im starting to write that one out .
Originally posted by: ggavinmoss
This is pretty easy, think about it. Here's a hint, you'll use / and %.
-geoff
Originally posted by: MCrusty
static long reverse(long a)
{
StringBuffer t = new StringBuffer();
while (a!=0)
{
t.append(a%10);
a/=10;
}
return Long.parseLong(t.toString());
}
Originally posted by: Ameesh
Originally posted by: MCrusty
static long reverse(long a)
{
StringBuffer t = new StringBuffer();
while (a!=0)
{
t.append(a%10);
a/=10;
}
return Long.parseLong(t.toString());
}
you dont need to store it in a string, just keep everything as primitive types
Originally posted by: MCrusty
How else would you store it? StringBuffers seem to be the most efficient way to store for something this trivial.
Originally posted by: FeathersMcGraw
Originally posted by: MCrusty
How else would you store it? StringBuffers seem to be the most efficient way to store for something this trivial.
The result can be accumulated as an integer/long type, as long as maintaining leading zeroes is not significant.