Here is my solution Jon Galloway's puzzle (my guess was "tens of numbers"):
1 Module Module1
2
3 Sub Main()
4
5 Dim testValue As Integer = 21
6 Const maxTestValue As Integer = 1000000
7 Dim count As Integer = 0
8
9 Do
10
11 If testValue Mod 10 <> 0 Then
12 Dim testString As String = testValue.ToString
13 Dim reverseString As String = Reverse(testString)
14 If reverseString < testString Then
15 Dim reverseValue As Integer = CInt(reverseString)
16 If testValue Mod reverseValue = 0 Then
17 Console.WriteLine(String.Format("{0} / {1} = {2}", testValue, reverseValue, testValue / reverseValue))
18 count += 1
19 End If
20 End If
21 End If
22
23 testValue += 1
24
25 Loop Until testValue > maxTestValue
26
27 Console.WriteLine(String.Format("{0} numbers <= {1} are non-trivially divisible by their reverse.", count, maxTestValue))
28 Console.ReadLine()
29
30 End Sub
31
32 Private Function Reverse(ByVal value As String) As String
33 Dim chars() As Char = value.ToCharArray
34 Array.Reverse(chars)
35 Return New String(chars)
36 End Function
37
38 End Module
The .NET limitation is that the String class has no Reverse method!
This gives the following rather interesting result:
It is easily seen that (only) numbers of the format "879*12" and "989*01" (using regex * = 0-or-more occurrences) are solutions to this problem.
I have found a rather elegant proof of this but unfortunately it is too large to fit in the margin ;-)
I wonder how this generalizes for bases other than 10.
Update 9/11/06:
Jon's solution post shows I should have taken the sequence a bit further before jumping to conclusions:
The pattern now looks like: (879*12)+ or (989*01)+
Reminds me of this old puzzle:
If you have n points on the circumference of a circle, and join them all together with straight lines, how many regions are outlined? (More than two lines may not cross at the same point.)
1 point => 1 region
2 points => 2 regions
3 points => 4 regions
4 points => 8 regions
n points => 2^(n-1) regions, right?
No comments:
Post a Comment