• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reverse 5 digit number by multiplying 4

 
Rakesh Joshi
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
-------
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
21978
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ryan McGuire
Ranch Hand
Posts: 1055
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred Joly
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- 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...
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A great performance-improvement would be to reduce the loop to unique digits in the numbers:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic