• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reverse 5 digit number by multiplying 4

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
-------
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
21978
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 55
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A great performance-improvement would be to reduce the loop to unique digits in the numbers:
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch That looks good for a solution with a loop. I trust it took you less than 15 years to find
 
reply
    Bookmark Topic Watch Topic
  • New Topic