Pi day

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
Gunslinger
Posts: 103
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Pi day

Post by Gunslinger »

For Pi day i made this code.
have fun

Code: Select all

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
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Pi day

Post by counting_pine »

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: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Pi day

Post by dodicat »

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

Code: Select all

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 
But an interesting take on pi anyway.
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Re: Pi day

Post by integer »

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

Code: Select all

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
What was your source/basis for this algo?
Gunslinger
Posts: 103
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: Pi day

Post by Gunslinger »

Thanks for the code updates.

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

https://www.youtube.com/watch?v=HrRMnzANHHs
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: 103
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: Pi day

Post by Gunslinger »

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.

https://www.youtube.com/watch?v=HrRMnzANHHs
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.
https://www.youtube.com/watch?v=RZBhSi_PwHU
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Pi day

Post by counting_pine »

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.
Post Reply