List all the numbers (below 4000) that their Roman numeral representations have the same number of letters as their squares.
Not much had to be changed in the roman number formation algorithm from the QuickBasic version I already had: the variable r$ had to be changed to ro$, as Visual Basic doesn't allow r as a number and r$ as a string, since the $ is not considered part of the name in Visual Basic, but rather as a type declaration that need be specified only once.
Actually, the below will give spurious results above about 63 (it doesn't show any results there) as it doesn't consider overlining for numbers higher than 4000, as the squares would be for those numbers.
DefDbl A-Z
Dim crlf$
Function mform$(x, t$)
a$ = Format$(x, t$)
If Len(a$) < Len(t$) Then a$ = Space$(Len(t$) - Len(a$)) & a$
mform$ = a$
End Function
Function pad$(x$, l)
pad$ = Left(x + Space$(l), l)
End Function
Private Sub Form_Load()
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
Form1.Visible = True
DoEvents
For i = 1 To 4000
rm$ = roman(i)
l = Len(rm$)
l2 = Len(roman(i * i))
If l = l2 Then
Text1.Text = Text1.Text & mform(i, "###0") & mform(i * i, "#####0") & " "
Text1.Text = Text1.Text & pad(rm$, 8) & " " & pad(roman(i * i), 8) & crlf
End If
DoEvents
Next
Text1.Text = Text1.Text & "done" & crlf
End Sub
Function roman$(n)
q = n \ 1000: r = n Mod 1000
ro$ = String$(q, "M")
n2 = r
q = n2 \ 100: r = n2 Mod 100
Select Case q
Case 9: ro$ = ro$ + "CM"
Case 5 To 8: ro$ = ro$ + "D" + String$(q - 5, "C")
Case 4: ro$ = ro$ + "CD"
Case 0 To 3: ro$ = ro$ + String$(q, "C")
End Select
n2 = r
q = n2 \ 10: r = n2 Mod 10
Select Case q
Case 9: ro$ = ro$ + "XC"
Case 5 To 8: ro$ = ro$ + "L" + String$(q - 5, "X")
Case 4: ro$ = ro$ + "XL"
Case 0 To 3: ro$ = ro$ + String$(q, "X")
End Select
n2 = r
q = n2
Select Case q
Case 9: ro$ = ro$ + "IX"
Case 5 To 8: ro$ = ro$ + "V" + String$(q - 5, "I")
Case 4: ro$ = ro$ + "IV"
Case 0 To 3: ro$ = ro$ + String$(q, "I")
End Select
roman$ = ro$
End Function
with the following results:
1 1 I I
2 4 II IV
8 64 VIII LXIV
10 100 X C
20 400 XX CD
23 529 XXIII DXXIX
32 1024 XXXII MXXIV
34 1156 XXXIV MCLVI
38 1444 XXXVIII MCDXLIV
39 1521 XXXIX MDXXI
|
Posted by Charlie
on 2015-05-22 10:52:38 |