A Curzon number is defined to be a positive integer n for which 2n + 1 is evenly divisible by 2 × n + 1.

Generalized Curzon numbers are those where the positive integer n, using a base integer k, satisfy the condition that kn + 1 is evenly divisible by k × n + 1.

Base here does not imply the radix of the counting system; rather the integer the equation is based on. All calculations should be done in base 10.

Generalized Curzon numbers only exist for even base integers.



Task 
 Find and show the first 50 Generalized Curzon numbers for even base integers from 2 through 10.


Stretch
 Find and show the one thousandth.


See also
 Numbers Aplenty - Curzon numbers
 OEIS:A224486 - Numbers k such that 2*k+1 divides 2^k+1 (Curzon numbers)
and even though it is not specifically mentioned that they are Curzon numbers:

 OEIS:A230076 - (A007521(n)-1)/4 (Generalized Curzon numbers with a base 4)




' 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