declare function gcd(a as uinteger, b as uinteger) as uinteger
dim as ulongInt prime1, count
dim as ulongInt ii=1
dim as uinteger ints = &hFFFF
screen 19
do
if gcd(int(rnd*ints+1), int(rnd*ints+1)) = 1 then prime1 +=1
count += 1
if count mod 10000 = 0 then print ii,"- Pi: "; sqr(6/(prime1 / count)), count, prime1: ii += 1
loop until inkey$ = "q" or count = 9223372036854775807
sleep
function gcd(a as uinteger, b as uinteger) as uinteger
dim as double an, bn
dim as ushort nn
if a < b then nn = a else nn = b
for n as ushort = nn to 1 step -1
an = a/n
bn = b/n
if an = int(an) andalso bn = int(bn) then return n
next
return 0
end function
Cool. I'm not sure exactly how it does it at this point, but it's evidently printing approximations of Pi.
(EDIT: So from this code, I'm guessing the probability that two random integers both in the range 1..N are coprime with probability 6/pi^2, and so the code is using that to calculate approximations of pi to increasing accuracy. Interesting.)
Note though, that your gcd function can be massively sped up with this fairly simple drop-in replacement:
declare function gcd(a as uinteger, b as uinteger) as uinteger
dim as ulongInt prime1, count
dim as ulongInt ii=1
dim as uinteger ints = &hFFFF
screen 19
do
if gcd(int(rnd*ints+1), int(rnd*ints+1)) = 1 then prime1 +=1
count += 1
if count mod 10000 = 0 then print ii,"- Pi: "; sqr(6/(prime1 / count)), count, prime1: ii += 1
loop until inkey$ = "q" or count = 9223372036854775807
sleep
Function gcd(a As Uinteger,b As Uinteger) As Uinteger
If b>a Then Swap b,a
Dim As Uinteger c
While b
c = a
a = b
b = c Mod b
Wend
Return a
End Function
'========================================================
'========================================================
function gcdgunslinger(a as uinteger, b as uinteger) as uinteger
dim as double an, bn
dim as ushort nn
if a < b then nn = a else nn = b
for n as ushort = nn to 1 step -1
an = a/n
bn = b/n
if an = int(an) andalso bn = int(bn) then return n
next
return 0
end function
declare function gcd(a as uinteger, b as uinteger) as uinteger
dim as ulongInt prime1, count
dim as ulongInt ii=1
dim as uinteger ints = &hFFFF
screen 19
do
if gcd(int(rnd*ints+1), int(rnd*ints+1)) = 1 then prime1 +=1
count += 1
if count mod 10000 = 0 then print ii,"- Pi: "; sqr(6/(prime1 / count)), count, prime1: ii += 1
loop until inkey$ = "q" or count = 9223372036854775807
sleep
function gcd(a as uinteger, b as uinteger) as uinteger
dim as double an, bn
dim as ushort nn
if a < b then nn = a else nn = b
for n as ushort = nn to 1 step -1
an = a/n
bn = b/n
if an = int(an) andalso bn = int(bn) then return n
next
return 0
end function
I know it's nice to always tell your source.
But when i wrote this i did that from my one memory and i had trouble finding that a co-prime function so i made up my one fast.
I know it's nice to always tell your source.
But when i wrote this i did that from my one memory and i had trouble finding that a co-prime function so i made up my one fast.
Just to say, sorry, I've had to approve your last few posts for some reason. I've removed you manually from the Newly Registered group, so hopefully that shouldn't happen anymore.