Problem 55 asks:
‘If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.
‘
‘Not all numbers produce palindromes so quickly. For example,
‘
‘349 + 943 = 1292,
‘1292 + 2921 = 4213
‘4213 + 3124 = 7337
‘
‘That is, 349 took three iterations to arrive at a palindrome.
‘
‘Although no one has proved it yet, it is thought that some numbers, like
‘196, never produce a palindrome. A number that never forms a palindrome
‘through the reverse and add process is called a Lychrel number. Due to the
‘theoretical nature of these numbers, and for the purpose of this problem,
‘we shall assume that a number is Lychrel until proven otherwise. In addition
‘you are given that for every number below ten-thousand, it will either
‘(i) become a palindrome in less than fifty iterations, or, (ii) no one, with
‘all the computing power that exists, has managed so far to map it to a
‘palindrome. In fact, 10677 is the first number to be shown to require over
‘fifty iterations before producing a palindrome:
‘4668731596684224866951378664 (53 iterations, 28-digits).
‘
‘Surprisingly, there are palindromic numbers that are themselves Lychrel
‘numbers; the first example is 4994.
‘
‘How many Lychrel numbers are there below ten-thousand?
‘
‘NOTE: Wording was modified slightly on 24 April 2007 to emphasise
‘the theoretical nature of Lychrel numbers.
‘
‘Not all numbers produce palindromes so quickly. For example,
‘
‘349 + 943 = 1292,
‘1292 + 2921 = 4213
‘4213 + 3124 = 7337
‘
‘That is, 349 took three iterations to arrive at a palindrome.
‘
‘Although no one has proved it yet, it is thought that some numbers, like
‘196, never produce a palindrome. A number that never forms a palindrome
‘through the reverse and add process is called a Lychrel number. Due to the
‘theoretical nature of these numbers, and for the purpose of this problem,
‘we shall assume that a number is Lychrel until proven otherwise. In addition
‘you are given that for every number below ten-thousand, it will either
‘(i) become a palindrome in less than fifty iterations, or, (ii) no one, with
‘all the computing power that exists, has managed so far to map it to a
‘palindrome. In fact, 10677 is the first number to be shown to require over
‘fifty iterations before producing a palindrome:
‘4668731596684224866951378664 (53 iterations, 28-digits).
‘
‘Surprisingly, there are palindromic numbers that are themselves Lychrel
‘numbers; the first example is 4994.
‘
‘How many Lychrel numbers are there below ten-thousand?
‘
‘NOTE: Wording was modified slightly on 24 April 2007 to emphasise
‘the theoretical nature of Lychrel numbers.
Euler problems should calculate under a minute. This one takes about 1.3 seconds.
Here is my code:
Option Explicit
Option Base 1
Sub Problem_055()
Dim i As Long
Dim j As Long
Dim T As Single
Dim n As String
Dim n_rev As String
Dim num As String
Dim Answer As Long
Dim Max As Long
Dim Last As Long
Dim IsTest As Boolean
Dim Series(5) As String
T = Timer ‘start timing
IsTest = True
If IsTest Then ‘to test the example cases
Max = 5
Last = 53
Series(1) = “47”
Series(2) = “196” ‘Lychrel
Series(3) = “349”
Series(4) = “4994” ‘Lychrel
Series(5) = “10677”
Else
Max = 9999 ‘ less than 10,000
Last = 50
end if
For j = 1 To Max
If IsTest Then
n = Series(j)
Else
n = CStr(j)
End If
For i = 1 To last ‘to test 10677
n_rev = StrReverse(n)
num = AddAsStrings(n, n_rev)
If num = StrReverse(num) Then ‘not a Lychrel number
Exit For
End If
n = num
If i = last Then
Answer = Answer + 1
End If
Next i
Next j
Debug.Print Answer; ” Time:”; Timer – T
End Sub
Option Base 1
Sub Problem_055()
Dim i As Long
Dim j As Long
Dim T As Single
Dim n As String
Dim n_rev As String
Dim num As String
Dim Answer As Long
Dim Max As Long
Dim Last As Long
Dim IsTest As Boolean
Dim Series(5) As String
T = Timer ‘start timing
IsTest = True
If IsTest Then ‘to test the example cases
Max = 5
Last = 53
Series(1) = “47”
Series(2) = “196” ‘Lychrel
Series(3) = “349”
Series(4) = “4994” ‘Lychrel
Series(5) = “10677”
Else
Max = 9999 ‘ less than 10,000
Last = 50
end if
For j = 1 To Max
If IsTest Then
n = Series(j)
Else
n = CStr(j)
End If
For i = 1 To last ‘to test 10677
n_rev = StrReverse(n)
num = AddAsStrings(n, n_rev)
If num = StrReverse(num) Then ‘not a Lychrel number
Exit For
End If
n = num
If i = last Then
Answer = Answer + 1
End If
Next i
Next j
Debug.Print Answer; ” Time:”; Timer – T
End Sub
It uses the AddAsStrings() function from Problem 16. It exits the loop if the addition is equal to its reverse.
Of course, the real question, is why anybody cares about Lychrel numbers, and who would make them their life’s work ;-)
…mrt
Hmmm…better set IsTest to False to prevent a correct wrong answer.
Happy New Year to all!
…mrt