RosettaCodeData/Task/Curzon-numbers/FreeBASIC/curzon-numbers-1.basic
2023-07-01 13:44:08 -04:00

42 lines
976 B
Text

' limit: k * n +1 must be smaller then 2^32-1
Function pow_mod(b As ULongInt, power As ULongInt, modulus As ULongInt) As ULongInt
' returns b ^ power mod modulus
Dim As ULongInt x = 1
While power > 0
If (power And 1) = 1 Then
x = (x * b) Mod modulus
End If
b = (b * b) Mod modulus
power = power Shr 1
Wend
Return x
End Function
For k As ULongInt= 2 To 10 Step 2
Print "The first 50 Curzon numbers using a base of "; k; ":"
Dim As ULongInt count, n = 1, p, m
Do
m = k * n +1
p = pow_mod(k, n ,m) +1
If p = m Then
count += 1
If count <= 50 Then
Print Using "#####"; n;
If (count Mod 10) = 0 Then Print
ElseIf count = 1000 Then
Print : Print "One thousandth: "; n
Print : Print
Exit Do
End If
End If
n += 1
Loop
Next
Sleep