## Pi day

User projects written in or related to FreeBASIC.
Gunslinger
Posts: 35
Joined: Mar 08, 2016 19:10

### Pi day

For Pi day i made this code.
have fun

Code: Select all

`declare function gcd(a as uinteger, b as uinteger) as uintegerdim as ulongInt prime1, countdim as ulongInt ii=1dim as uinteger ints = &hFFFFscreen 19do   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 += 1loop until inkey\$ = "q" or count = 9223372036854775807sleepfunction 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 0end function`
counting_pine
Posts: 6176
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

### Re: Pi day

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:

Code: Select all

`function gcd(a as uinteger, b as uinteger) as uinteger    if b > a then return gcd(b, a)    if b = 0 then return a    return gcd(b, a mod b)end function`

Instead of waiting a few seconds for each line to print, it will now print hundreds of lines a second!
dodicat
Posts: 6083
Joined: Jan 10, 2006 20:30
Location: Scotland

### Re: Pi day

Hi gunslinger, yes changing the gcd slightly could be Pi noon.

Code: Select all

`declare function gcd(a as uinteger, b as uinteger) as uintegerdim as ulongInt prime1, countdim as ulongInt ii=1dim as uinteger ints = &hFFFFscreen 19do   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 += 1loop until inkey\$ = "q" or count = 9223372036854775807sleepFunction 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 aEnd 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 0end function `

But an interesting take on pi anyway.
integer
Posts: 384
Joined: Feb 01, 2007 16:54
Location: usa

### Re: Pi day

Gunslinger wrote:For Pi day i made this code.
have fun

Code: Select all

`declare function gcd(a as uinteger, b as uinteger) as uintegerdim as ulongInt prime1, countdim as ulongInt ii=1dim as uinteger ints = &hFFFFscreen 19do   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 += 1loop until inkey\$ = "q" or count = 9223372036854775807sleepfunction 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 0end function`

What was your source/basis for this algo?
Gunslinger
Posts: 35
Joined: Mar 08, 2016 19:10

### Re: Pi day

I got the idea for a YouTube user.
For you i searched my history and here you go.

Matt Parker @ http://standupmaths.com
And he got it from Najeeb Sheikh and Jake Trookman (i think)

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.
Gunslinger
Posts: 35
Joined: Mar 08, 2016 19:10

### Re: Pi day

Gunslinger wrote:Thanks for the code updates.

I got the idea for a YouTube user.
For you i searched my history and here you go.

Matt Parker @ http://standupmaths.com
And he got it from Najeeb Sheikh and Jake Trookman (i think)

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.

counting_pine
Posts: 6176
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

### Re: Pi day

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.

### Who is online

Users browsing this forum: No registered users and 1 guest