Jump to content

Rare numbers: Difference between revisions

m
→‎{{header|FreeBASIC}}: made it more FreeBASIC, added simple test.
(Rare numbers en FreeBASIC)
m (→‎{{header|FreeBASIC}}: made it more FreeBASIC, added simple test.)
Line 1,246:
 
=={{header|FreeBASIC}}==
Made some changes and added a simple test to speed things up. Results in about 1 minute.
About 16.5 minutes to find the first 5 rare numbers
{{trans|Phix}}
<lang freebasic>#includeFunction "string.bi"revn(n As ULongInt, nd As ULongInt) As ULongInt
Dim As IntegerULongInt r = 0
#define floor(x) ((x*2.0-0.5) Shr 1)
For i As IntegerUInteger = 1 To nd
#define remainder(n, m) (n-m*Int(n/m))
r = r * 10 + remainder(n, Mod 10)
 
n = floor(n/ \ 10)
Function revn(n As Integer, nd As Integer) As Integer
Dim As Integer r = 0
For i As Integer = 1 To nd
r = r * 10 + remainder(n, 10)
n = floor(n/10)
Next i
Return r
End Function
 
Dim As IntegerUInteger nd = 2, count = 0, lim = 9990, n = 920
 
Do
n += 1
Dim As IntegerULongInt r = revn(n,nd)
If r < n Then
Dim As IntegerULongInt s = n + r, d = n - r
If s = (floor(Sqr(s)) ^ 2)nd And d = (floor(Sqr(d)) ^ 2)1 Then
If d Mod 1089 <> 0 Then GoTo jump
Else
If s Mod 121 <> 0 Then GoTo jump
End If
If Frac(Sqr(s)) = 0 And Frac(Sqr(d)) = 0 Then
count += 1
Print count; ": "; n
If count = 5 Then Exit Do : End If
End If
End If
jump:
If n = lim Then
lim = lim * 10 + 9
nd += 1
n = (lim \ 9) * 2
End If
Loop
Sleep</lang>
 
Print
Print "Done"
Sleep</lang>
{{out}}
<pre>1: 65
2: 621770
3: 281089082
4: 2022652202
5: 2042832002</pre>
 
=={{header|Go}}==
457

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.