# Inverting integer

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-17-2009, 10:12 PM
gusgol
Inverting integer
Hi people,

This is the problem:

I need to put the last digit from an integer number to its front.
Examples: 1234 --> 4123
5678 --> 8567

But is allowed only the use of integer variables in this transformation.

• 04-17-2009, 10:41 PM
xcallmejudasx
use a loop or two and convert it to a string. use modulus 10 (%10) to get the last number and then concat the remaining numbers to that.
• 04-17-2009, 10:59 PM
markw8500
That modulus idea is pretty slick...
• 04-17-2009, 11:01 PM
OrangeDog
Using only integer methods:

1234 % 10 = 4
1234 / 10 = 123
10 ^ lg 1234 = 1000

123 + 1000*4 = 4123

There isn't an integer implementation of lg, but you can use (int)Math.log10()
• 04-17-2009, 11:07 PM
markw8500
That is even more slick...
• 04-17-2009, 11:22 PM
OrangeDog
It's called computer programming
• 04-18-2009, 12:06 AM
markw8500
It's all computer programming... It's just that some are better than others...
• 04-18-2009, 02:10 PM
Tshegofatsom
• 04-18-2009, 03:26 PM
DavidG24
hey mate, the previous suggestions made will work perfectly fine, however I think the most efficient way is to covert your integer to a char array and reverse the array, i.e.
public static void main(String[] args) {
Integer a = 1234;
char[] b = a.toString().toCharArray();
String temp="";
for(int i = 0; i <b.length;i++)
temp += b[b.length - 1 - i];
System.out.println(temp);
}

Hope this helps,

David
• 04-18-2009, 03:56 PM
Eranga
Quote:

Originally Posted by markw8500
That is even more slick...

• 04-18-2009, 05:14 PM
OrangeDog
David and Tshegofatso,
As stated in the question, you're only allowed to use integer variables, and the task isn't to reverse the number, it's to move the last digit to the front.

And 6 mathematical operations is always going to be more efficient than mass of object manipulation David suggests. You haven't even used a StringBuffer to avoid the cost of repeated string concatenation, nor have you arranged you loop to count down and eliminate unneeded index calculation. If you wanted to reverse a char array (which isn't even the question) you can do it in place using either a single temp char variable or using XORs to swap the values.
• 04-18-2009, 05:42 PM
CJSLMAN
Quote:

Quote:
Originally Posted by markw8500

That is even more slick...
Quote:
Originally Posted by Eranga:

Eranga... I think you might have misunderstood Mark's comment. He is indicating that the suggestion by OrangeDog is a good one, neat, ingenious, etc.

CJSL
• 04-18-2009, 06:14 PM
markw8500
Eranga,
Chris is correct... At the time I posted that I was about to offer an answer that was long and cumbersome, but it worked... But right before I submitted it I checked to see that no one was doing the same thing I was offering... Then I saw OrangeDog's solution and was impressed... In my opinion, no one could create a better solution...
• 04-18-2009, 10:36 PM
pbrockway2
Quote:

Originally Posted by OrangeDog
David and Tshegofatso,
As stated in the question, you're only allowed to use integer variables, and the task isn't to reverse the number, it's to move the last digit to the front.

And 6 mathematical operations is always going to be more efficient than mass of object manipulation David suggests.

Just my 2c worth...

I thought when this was first posted that the question was ok as an exercise in using operators that a student may be unfamiliar with (%, eg), but that the restriction to integer variables was arbitrary and a bit silly. (basically the teacher's laziness in not coming up with a more robust question.)

The operation of forming a new numeral from a given one by moving the last digit to the beginning is essentially a String - or CharacterSequence - operation. The operation can be performed alike on the decimal form of a integer, a phone "number", or even a person's name.

Expressing data using a reasonable type is more important than efficiency, imho. And numerals are strings, not numbers.
• 04-18-2009, 10:53 PM
OrangeDog
Being able to do this sort of thing when you have to program in restricted situations - either or all of memory, time and instruction limitations - is a valuable skill.
Don't criticise interesting intellectual questions just because they don't match up to your practical experience.
• 04-19-2009, 12:27 AM
CJSLMAN
If I'd been the teacher, I would had requested both integer and string implementations... just to show that there's more than one way to get the same result.

CJSL
• 04-19-2009, 12:40 AM
OrangeDog
How do you know they didn't? Just because it didn't get posted here...
• 04-19-2009, 03:40 AM
Eranga
Quote:

Originally Posted by CJSLMAN
Eranga... I think you might have misunderstood Mark's comment. He is indicating that the suggestion by OrangeDog is a good one, neat, ingenious, etc.

CJSL

Yes Chris I got what he trying to say.... that he wants to say that' the best of best, slick.

What I want to say is, if he has another explanation why don't he post it here, just commenting in that way. May be it's not fair to say that by me, but I don't like that. He's not the thread starter, but he's commenting twice, nothing else. I don't like that. ;)
• 04-19-2009, 03:43 AM
Eranga
Quote:

Originally Posted by markw8500
Eranga,
Chris is correct... At the time I posted that I was about to offer an answer that was long and cumbersome, but it worked... But right before I submitted it I checked to see that no one was doing the same thing I was offering... Then I saw OrangeDog's solution and was impressed... In my opinion, no one could create a better solution...

Sorry if I make my comment is mess-up to you. What I want to say is, just commenting what's the best is not make sense. In your first three posts what you say only is, this is better than the other and so on... :rolleyes:
• 04-19-2009, 04:43 AM
DavidG24
OrangeDog,

I misread the question, hope I didn't step on any toes!, yes for an integer of logarithmic order n, the modulus operator coupled with the multiplication operator to yield 2(n-1) operations would be the most efficient approach.

Just out of curiosity, consider string operations were permitted do you think this would hold for integers with orders > 1e10 or higher, would it not be more efficient to cast to a string and 'rearrange' the string and cast back to an int?

Once again, I don't mean to offend - just curious,

David
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last