Win a copy of Java Challengers this week in the Java in General forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• paul wheaton
• Devaka Cooray
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Liutauras Vilda
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Piet Souris
Bartenders:
• salvin francis
• Mikalai Zaikin
• Himai Minh

# Reverse 5 digit number by multiplying 4

Ranch Hand
Posts: 218
• Number of slices to send:
Optional 'thank-you' note:
A, B, C, D and E are 5 different single digit numbers. The number ABCDE times 4 will have a result of EDCBA. What digit numbers do A, B, C, D, and E represent?

ABCDE
* 4
-------
EDCBA
-------

Ranch Hand
Posts: 1296
• Number of slices to send:
Optional 'thank-you' note:
21978

Ranch Hand
Posts: 1249
• Number of slices to send:
Optional 'thank-you' note:
Could any one tell me what is the logic to calculate this problem. Atleast direct others how to calculate this problem or tell the algorithm to solve it....

Garrett Rowe
Ranch Hand
Posts: 1296
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Ankur Sharma:
Could any one tell me what is the logic to calculate this problem. Atleast direct others how to calculate this problem or tell the algorithm to solve it....

21978 * 4 = 87912

I just used a simple loop from 10000 to 99999 and multiplied each number by 4, converted to a String, and checked to see if the String reversed was equal to the original number as a String.

Bartender
Posts: 1202
22
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Ankur Sharma:
Could any one tell me what is the logic to calculate this problem. Atleast direct others how to calculate this problem or tell the algorithm to solve it....

You could look at it as an integer linear programming problem.

Find A,B,C,D,E such that.

A*10000 + B*1000 + C*100 + D*10 + E = 4*(E*10000 + D*1000 + C*100 + B*10 + A)
- or -
A*9996 + B*960 - C*300 - D*3990 - E*39999 = 0
A >= 0
A <= 9
B >= 0
B <= 9
C >= 0
C <= 9
D >= 0
D <= 9
E >= 0
E <= 9

Ranch Hand
Posts: 55
• 1
• Number of slices to send:
Optional 'thank-you' note:
- A can only be 1 or 2
if A > 2 then the answer would be of the form FEDCBA

-let's try with A = 1
1BCDE
*4
------
EDCB1

impossible : 4*E = some number ending by 1

- then A "MUST" be 2
2BCDE
*4
-----
EDCB2

- E=3 or E=8 (E*4 has to be a number ending with 2)

let's try E=8 (looks like the easier...)

2BCD8
*4
-----
8DCB2

- B can only 1 or 2 (otherwise 4*B > 10 and that's now impossible)
Since we already have a 2 , b=1

21CD8
*4
-----
8DC12

- we can write : D*4 + 3 = some number ending by 1
->D*4 = some number ending by 8

D=2 or D=7 => D=7 (A = already 2)

21C78
*4
-----
87C12

- (4*21000 = 84000)
we can write

C78
*4
-----
3C12

we can write C*4 + 3 = 3C (beware 3C means thirty C)
so C >= 7
we already have a 7 and a 8 => C = 9

21978
*4
-----
87912

No computer needed to do this...

Ranch Hand
Posts: 1026
• Number of slices to send:
Optional 'thank-you' note:

Ranch Hand
Posts: 1923
• Number of slices to send:
Optional 'thank-you' note:
A great performance-improvement would be to reduce the loop to unique digits in the numbers:

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
Microsoft's VBA:

For x = 10000 To 25000
a\$ = Trim(Str(x))
Reverse_a\$ = ""
For y = Len(x) To 1 Step -1
Reverse_a\$ = Trim(Reverse_a\$ + Mid(a\$, y, 1))
Next
If Reverse_a\$ = Trim(Str(x * 4)) Then MsgBox (a\$)
Next x

Marshal
Posts: 72657
317
• Number of slices to send:
Optional 'thank-you' note:
Welcome to the Ranch That looks good for a solution with a loop. I trust it took you less than 15 years to find

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.