Start with an integer n. Multiply its smallest and largest digits together and add their product to n, forming the second term in the sequence. Repeat this process until n stops changing.
Example: 231, 234, 242, 250, 250. This is a 4-term sequence.
What is the longest sequence for starting values less than 30,000?
DefDbl A-Z
Dim crlf$, h(100)
Private Sub Form_Load()
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
Form1.Visible = True
For n = 1 To 30000
st = 0
h(st) = n
Do
ns$ = LTrim(Str(h(st)))
lowd = 9: highd = 0
For i = 1 To Len(ns)
If Val(Mid(ns, i, 1)) < lowd Then lowd = Val(Mid(ns, i, 1))
If Val(Mid(ns, i, 1)) > highd Then highd = Val(Mid(ns, i, 1))
Next
addend = lowd * highd
If addend = 0 Then Exit Do
st = st + 1
h(st) = h(st - 1) + addend
Loop Until addend = 0
If st > Max Then
Max = st
Text1.Text = Text1.Text & st & ": "
For i = 0 To st
Text1.Text = Text1.Text & Str(h(i))
Next
Text1.Text = Text1.Text & crlf & crlf
DoEvents
End If
Next n
Max = 0
For n = -30000 To -1
st = 0
h(st) = n
Do
ns$ = LTrim(Str(h(st)))
lowd = 9: highd = 0
For i = 1 To Len(ns)
If Mid(ns, i, 1) <> "-" Then
If Val(Mid(ns, i, 1)) < lowd Then lowd = Val(Mid(ns, i, 1))
If Val(Mid(ns, i, 1)) > highd Then highd = Val(Mid(ns, i, 1))
End If
Next
addend = lowd * highd
If addend = 0 Then Exit Do
st = st + 1
h(st) = h(st - 1) + addend
Loop Until addend = 0
If st > Max Then
Max = st
Text1.Text = Text1.Text & st & ": "
For i = 0 To st
Text1.Text = Text1.Text & Str(h(i))
Next
Text1.Text = Text1.Text & crlf & crlf
DoEvents
End If
Next n
Text1.Text = Text1.Text & " done"
End Sub
finds successively larger longest sequences:
4: 1 2 6 42 50
8: 3 12 14 18 26 38 62 74 102
9: 7 56 86 134 138 146 152 157 164 170
12: 53 68 116 122 124 128 136 142 146 152 157 164 170
13: 449 485 517 524 534 549 585 625 637 658 698 752 766 808
18: 487 519 528 544 564 588 628 644 668 716 723 737 758 798 861 869 923 941 950
20: 2539 2557 2571 2578 2594 2612 2618 2626 2638 2654 2666 2678 2694 2712 2719 2728 2744 2758 2774 2788 2804
29: 3399 3426 3438 3462 3474 3495 3522 3532 3542 3552 3562 3574 3595 3622 3634 3652 3664 3682 3698 3725 3739 3766 3787 3811 3819 3828 3844 3868 3892 3910
30: 3414 3418 3426 3438 3462 3474 3495 3522 3532 3542 3552 3562 3574 3595 3622 3634 3652 3664 3682 3698 3725 3739 3766 3787 3811 3819 3828 3844 3868 3892 3910
47: 26212 26218 26226 26238 26254 26266 26278 26294 26312 26318 26326 26338 26354 26366 26378 26394 26412 26418 26426 26438 26454 26466 26478 26494 26512 26518 26526 26538 26554 26566 26578 26594 26612 26618 26626 26638 26654 26666 26678 26694 26712 26719 26728 26744 26758 26774 26788 26804
53: 28217 28225 28241 28249 28267 28283 28299 28317 28325 28341 28349 28367 28383 28399 28417 28425 28441 28449 28467 28483 28499 28517 28525 28541 28549 28567 28583 28599 28617 28625 28641 28649 28667 28683 28699 28717 28725 28741 28749 28767 28783 28799 28817 28825 28841 28849 28867 28883 28899 28917 28926 28944 28962 28980
The longest sequence found starting under 30,000 is the above sequence of 53, starting at 28217, not counting the terminal 28980.
Just for the heck of it, we try starting with a minimum of -30000:
6: -29999-29981-29972-29954-29936-29918-29909
7: -29193-29184-29175-29166-29157-29148-29139-29130
8: -29192-29183-29174-29165-29156-29147-29138-29129-29120
9: -29191-29182-29173-29164-29155-29146-29137-29128-29119-29110
The longest is only 9, so limiting to positive integers is no hindrance.
|
Posted by Charlie
on 2015-01-29 14:41:58 |