A nine digit number has the property where the first digit equals the number of zeros and ones used in the number, the second digit equals the number of ones and twos used in the number, the third digit equals the number of twos and threes used in the number, etc. through the ninth digit equals the number of eights and nines used in the number. What could the number be?
A ten digit number has a similar property to the nine digit number. The first digit equals the number of zeros and ones used in the number, the second digit equals the number of ones and twos used in the number, etc. through the ninth digit. And also, the tenth digit equals the number of zeros and nines used in the number. What could this number be?
(In reply to
Computer program solution by Penny)
Penny, I liked your method--especially the insight that the sum of all the digits can't be more than 20; which helps optimize the cases you need to look at.
But, obsessive as I am, I had to try to see if I could improve the performance. The following program, using basically your same method, runs on my machine in 28 seconds:
Option Strict On
Imports System.Math
Module Module1
Dim i1 As Integer
Dim i2 As Integer
Dim i3 As Integer
Dim i4 As Integer
Dim i5 As Integer
Dim i6 As Integer
Dim i7 As Integer
Dim i8 As Integer
Dim i9 As Integer
Dim i10 As Integer
Sub main()
Dim startTime As DateTime = Now
For i1 = 0 To 9
For i2 = 0 To 9
For i3 = 0 To Min(9, 20 - i1 - i2)
For i4 = 0 To Min(9, 20 - i1 - i2 - i3)
For i5 = 0 To Min(9, 20 - i1 - i2 - i3 - i4)
For i6 = 0 To Min(9, 20 - i1 - i2 - i3 - i4 - i5)
For i7 = 0 To Min(9, 20 - i1 - i2 - i3 - i4 - i5 - i6)
For i8 = 0 To Min(9, 20 - i1 - i2 - i3 - i4 - i5 - i6 - i7)
For i9 = 0 To Min(9, 20 - i1 - i2 - i3 - i4 - i5 - i6 - i7 - i8)
check9()
For i10 = 0 To Min(9, 20 - i1 - i2 - i3 - i4 - i5 - i6 - i7 - i8 - i9)
check10()
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
Console.WriteLine()
Console.WriteLine("Seconds elapsed: {0}", DateDiff(DateInterval.Second, startTime, Now))
Console.ReadLine()
End Sub
Sub check9()
Dim digits(10) As Integer
digits(i1) += 1 : digits(i1 + 1) += 1
digits(i2) += 1 : digits(i2 + 1) += 1
digits(i3) += 1 : digits(i3 + 1) += 1
digits(i4) += 1 : digits(i4 + 1) += 1
digits(i5) += 1 : digits(i5 + 1) += 1
digits(i6) += 1 : digits(i6 + 1) += 1
digits(i7) += 1 : digits(i7 + 1) += 1
digits(i8) += 1 : digits(i8 + 1) += 1
digits(i9) += 1 : digits(i9 + 1) += 1
If i1 <> digits(1) Then Return
If i2 <> digits(2) Then Return
If i3 <> digits(3) Then Return
If i4 <> digits(4) Then Return
If i5 <> digits(5) Then Return
If i6 <> digits(6) Then Return
If i7 <> digits(7) Then Return
If i8 <> digits(8) Then Return
If i9 <> digits(9) Then Return
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7} {8}", i1, i2, i3, i4, i5, i6, i7, i8, i9)
End Sub
Sub check10()
Dim digits(11) As Integer
digits(i1) += 1 : digits(i1 + 1) += 1
digits(i2) += 1 : digits(i2 + 1) += 1
digits(i3) += 1 : digits(i3 + 1) += 1
digits(i4) += 1 : digits(i4 + 1) += 1
digits(i5) += 1 : digits(i5 + 1) += 1
digits(i6) += 1 : digits(i6 + 1) += 1
digits(i7) += 1 : digits(i7 + 1) += 1
digits(i8) += 1 : digits(i8 + 1) += 1
digits(i9) += 1 : digits(i9 + 1) += 1
digits(i10) += 1 : digits(i10 + 1) += 1
digits(10) += digits(0) 'adds the count of 0's to the count of 9's.
If i1 <> digits(1) Then Return
If i2 <> digits(2) Then Return
If i3 <> digits(3) Then Return
If i4 <> digits(4) Then Return
If i5 <> digits(5) Then Return
If i6 <> digits(6) Then Return
If i7 <> digits(7) Then Return
If i8 <> digits(8) Then Return
If i9 <> digits(9) Then Return
If i10 <> digits(10) Then Return
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}", i1, i2, i3, i4, i5, i6, i7, i8, i9, i10)
End Sub
End Module