Here is how the "Happy Number" series is generated. Take any integer. Form the next term by adding together the sum of the squares of each of the term's digits. The succeeding terms are all generated in the same way. Continue until one of the terms is either 4 or 1. If it ends in 4, a continuous loop exists. If it ends in 1, the series terminates and the first term in such a series is known as a Happy Number.
Example: 49, 97, 130, 10, 1.
Find a series of 5 consecutive integers which are all Happy Numbers.
DefDbl A-Z
Dim crlf$, ns As String, sepsplaced, prevsep, stot, prod
Private Sub Form_Load()
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
Form1.Visible = True
For i = 1 To 1000000
If happy(i) Then
hct = hct + 1
If hct > 4 Then
Text1.Text = Text1.Text & i - hct + 1 & "-" & i & Str(hct) & crlf
For j = i - hct + 1 To i
showHappy j
Next
End If
Else
hct = 0
End If
DoEvents
Next
Text1.Text = Text1.Text & "done"
End Sub
Function happy(x)
t = x
Do
If t = 1 Then happy = 1: Exit Function
If t = 4 Then happy = 0: Exit Function
ts$ = LTrim(Str(t))
t = 0
For i = 1 To Len(ts)
d = Val(Mid(ts, i, 1))
t = t + d * d
Next
Loop
End Function
Sub showHappy(x)
t = x
Do
Text1.Text = Text1.Text & Str(t)
If t = 1 Then Exit Do
If t = 4 Then Exit Do
ts$ = LTrim(Str(t))
t = 0
For i = 1 To Len(ts)
d = Val(Mid(ts, i, 1))
t = t + d * d
Next
Loop
Text1.Text = Text1.Text & crlf
End Sub
finds these series of five:
44488-44492 5
44488 176 86 100 1
44489 193 91 82 68 100 1
44490 129 86 100 1
44491 130 10 1
44492 133 19 82 68 100 1
222688-222692 5
222688 176 86 100 1
222689 193 91 82 68 100 1
222690 129 86 100 1
222691 130 10 1
222692 133 19 82 68 100 1
226288-226292 5
226288 176 86 100 1
226289 193 91 82 68 100 1
226290 129 86 100 1
226291 130 10 1
226292 133 19 82 68 100 1
258598-258602 5
258598 263 49 97 130 10 1
258599 280 68 100 1
258600 129 86 100 1
258601 130 10 1
258602 133 19 82 68 100 1
262288-262292 5
262288 176 86 100 1
262289 193 91 82 68 100 1
262290 129 86 100 1
262291 130 10 1
262292 133 19 82 68 100 1
285598-285602 5
285598 263 49 97 130 10 1
285599 280 68 100 1
285600 129 86 100 1
285601 130 10 1
285602 133 19 82 68 100 1
404488-404492 5
404488 176 86 100 1
404489 193 91 82 68 100 1
404490 129 86 100 1
404491 130 10 1
404492 133 19 82 68 100 1
440488-440492 5
440488 176 86 100 1
440489 193 91 82 68 100 1
440490 129 86 100 1
440491 130 10 1
440492 133 19 82 68 100 1
444088-444092 5
444088 176 86 100 1
444089 193 91 82 68 100 1
444090 129 86 100 1
444091 130 10 1
444092 133 19 82 68 100 1
528598-528602 5
528598 263 49 97 130 10 1
528599 280 68 100 1
528600 129 86 100 1
528601 130 10 1
528602 133 19 82 68 100 1
582598-582602 5
582598 263 49 97 130 10 1
582599 280 68 100 1
582600 129 86 100 1
582601 130 10 1
582602 133 19 82 68 100 1
622288-622292 5
622288 176 86 100 1
622289 193 91 82 68 100 1
622290 129 86 100 1
622291 130 10 1
622292 133 19 82 68 100 1
825598-825602 5
825598 263 49 97 130 10 1
825599 280 68 100 1
825600 129 86 100 1
825601 130 10 1
825602 133 19 82 68 100 1
852598-852602 5
852598 263 49 97 130 10 1
852599 280 68 100 1
852600 129 86 100 1
852601 130 10 1
852602 133 19 82 68 100 1
where each member's process leading to a digit 1 is shown below the identification of the series of 5. Any series of more than 5 would have been shown if present.
BTW, the loop referred to in the puzzle is 4, 16, 37, 58, 89, 145, 42, 20.
|
Posted by Charlie
on 2015-02-02 15:29:50 |