Ergo, any loop can be of the form:

for i = 9 to 9999 step 10 — use your favorite language syntax here

And then, of course, you don’t need to execute the “X mod 10 = 9” test. Further, since 9 is odd, you can skip the “X mod 2 = 1” test as well.

Last bit of tuning is to recognize that the modulo function is more likely to succeed for false answers for small modulus values than for large modulus values, thus the optimum series is “for modulus = 8 downto 3”.

Resulting in (pseudo code):

integer i integer m boolean bfound for i = 9 to 9999 step 10 bfound = true for m = 8 downto 3 step -1 if (i mod m) != (m-1) then bfound = false exit for ' exit inner loop on first modulus test failure end if next ' m if bfound then msgbox i exit for end if next ' i]]>

For i = 1 To 10000

If i Mod 10 = 9 And i Mod 9 = 8 And i Mod 8 = 7 And i Mod 7 = 6 And i Mod 6 = 5 And i Mod 5 = 4 And i Mod 4 = 3 And i Mod 3 = 2 And i Mod 2 = 1 Then

MsgBox i

End If

i = i + 1

Next

Dustin

]]>This was my first algorith (Oracle PL/SQL), just to check Flame’s answer ( ):

create or replace PROCEDURE test IS found boolean := false; theNumber number := 9; BEGIN while not found loop -- test all constraints if mod(theNumber,10) = 9 then if mod(theNumber,9) = 8 then if mod(theNumber,8) = 7 then if mod(theNumber,7) = 6 then if mod(theNumber,6) = 5 then if mod(theNumber,5) = 4 then if mod(theNumber,4) = 3 then if mod(theNumber,3) = 2 then if mod(theNumber,2) = 1 then dbms_output.put_line('The number is: '||theNumber); found := true; end if; end if; end if; end if; end if; end if; end if; end if; end if; theNumber := theNumber + 10; exit when theNumber > 100000; -- Just in case end loop; END;

**Then I changed it to a shorter one:**

while not found loop for i in 2 .. 9 loop if mod(theNumber,i) != i-1 then exit; elsif i = 9 then dbms_output.put_line('The number is: '||theNumber); found := true; end if; end loop; theNumber := theNumber + 10; exit when theNumber > 100000; -- Just in case end loop;]]>

I noticed Hendi used Java & RoadDust used MOD function. Is it possible to share the code – this will keep us informed of various techniques. I am copying my Excel/VB code for others to critiques. I set up Excel/VB to populate the number and the corresponding remainder values (from Row 10 and onwards). Then I compared the remainder pattern with “987654321” to check if the number is found.

I do realize this is not the most elegant programming code!

Sub DisplayNumber()

‘ Ctrl + Shft+ N is the shortcut to Run this macro

‘ Initial analysis reveals that the number must end with 9

‘ Also start with 89 & then increment by 90 so that remainder

‘ by dividing with 9 will be 8. This is just to save excessive display

‘ You can start with 9 & then increment by 10

X = 89

For I = 10 To 100

Cells(I, 1) = X

Y = 10

For J = 2 To 10

C = Int(X / Y)

R = X – (C * Y)

Cells(I, J) = R

Y = Y – 1

Next J

‘ Form the pattern & compare with 9876…

Cells(I, 11) = Cells(I, 2) & Cells(I, 3) & Cells(I, 4) & Cells(I, 5) & Cells(I, 6) & Cells(I, 7) & Cells(I, 8) & Cells(I, 9) & Cells(I, 10)

If (Cells(I, 11) = “987654321”) Then Cells(I, 12) = “Result”

X = X + 90

Next I

End Sub

]]>Dustin

]]>I used a brute force algorithm, and got the same answer.

]]>Going old school with the pencil and paper.

Nice

Sean

]]>Thanks for sharing this; I don’t think there’s anything wrong with the community helping to hone each other’s skills. I’ll have to try this later in Excel with VB as you mentioned. I’d love to see how other members figure this one out. Feel free to post your methods here!

Community Moderator