1. Member
Join Date
Apr 2009
Posts
3
Rep Power
0

## 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.

2. 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.

3. That modulus idea is pretty slick...

4. 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()
Last edited by OrangeDog; 04-17-2009 at 11:05 PM.

5. That is even more slick...

6. It's called computer programming

7. It's all computer programming... It's just that some are better than others...

8. Member
Join Date
Apr 2009
Location
Pretoria, Gauteng, South Africa
Posts
43
Rep Power
0

9. Member
Join Date
Apr 2009
Posts
49
Rep Power
0
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

10. 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.

11. 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

12. 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...

13. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
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.

14. 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.

15. ## Two roads to Rome?

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

16. How do you know they didn't? Just because it didn't get posted here...

17. 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. ;)

18. 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:

19. Member
Join Date
Apr 2009
Posts
49
Rep Power
0
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

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•