Rare numbers: Difference between revisions

Content added Content deleted
(Rare numbers en FreeBASIC)
m (→‎{{header|FreeBASIC}}: made it more FreeBASIC, added simple test.)
Line 1,246: Line 1,246:


=={{header|FreeBASIC}}==
=={{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}}
{{trans|Phix}}
<lang freebasic>#include "string.bi"
<lang freebasic>Function revn(n As ULongInt, nd As ULongInt) As ULongInt
Dim As ULongInt r
#define floor(x) ((x*2.0-0.5) Shr 1)
For i As UInteger = 1 To nd
#define remainder(n, m) (n-m*Int(n/m))
r = r * 10 + n Mod 10

n = 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
Next i
Return r
Return r
End Function
End Function


Dim As Integer nd = 2, count = 0, lim = 99, n = 9
Dim As UInteger nd = 2, count, lim = 90, n = 20


Do
Do
n += 1
n += 1
Dim As Integer r = revn(n,nd)
Dim As ULongInt r = revn(n,nd)
If r < n Then
If r < n Then
Dim As Integer s = n+r, d = n-r
Dim As ULongInt s = n + r, d = n - r
If s = (floor(Sqr(s)) ^ 2) And d = (floor(Sqr(d)) ^ 2) Then
If nd And 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
count += 1
Print count; ":"; n
Print count; ": "; n
If count = 5 Then Exit Do : End If
If count = 5 Then Exit Do : End If
End If
End If
End If
End If
jump:
If n = lim Then
If n = lim Then
lim = lim * 10 + 9
lim = lim * 10
nd += 1
nd += 1
n = (lim \ 9) * 2
End If
End If
Loop
Loop
Sleep</lang>


Print
Print "Done"
Sleep</lang>
{{out}}
<pre>1: 65
2: 621770
3: 281089082
4: 2022652202
5: 2042832002</pre>


=={{header|Go}}==
=={{header|Go}}==