Monday, November 29, 2010

Fractal Tree


graphsize 600,600
for i = 1 to 1000
x1=300
y1=600
s=290
angle = 270
color 127*int(rand*3),127*int(rand*3),127*int(rand*3)
for b = 1 to 6
gosub lines
s=s*.5
angle = angle + int(rand*5)*45-90
next b
next i
lines:
r = (angle/180)*pi
y2=y1 + (sin (r))*s
x2=x1 - (cos (r))*s
for n = 1 to 30
circle (x2*n+x1*(30-n))/30,(y2*n+y1*(30-n))/30,36/b^2
next n
x1=x2
y1=y2
return

Saturday, November 27, 2010

Nim

Rem take as many pieces you want from one of the piles . The one who takes the last piece wins
a=7
b=6
c=5
font "arial",20,100
gosub draw
do
Input "Which pile a b or c? ", p$
Input "How many pieces? ", p
If p$ = "a" then a=a-p
If p$ = "b" then b=b-p
If p$ = "c" then c=c-p
Print "Your play"
gosub draw
pause 3
if a+b+c=0 then
print "You win"
end
end if
Print "Computer play"
gosub Computerplay
gosub draw
until a+b+c=0
Print "I Won"
Computerplay:
play=0
n=0
do
n=n+1
ma=0:mb=0:mc=0
letter = int(rand*3)
if a>0 and letter = 0 then
ma = int(rand*a+1)
a= a-ma
end if
if b>0 and letter = 1 then
mb = int(rand*b+1)
b= b-mb
end if
if c>0 and letter = 2 then
mc = int(rand*c+1)
c= c-mc
end if
gosub test
if sum <>0 then
a=a+ma : b=b+mb : c=c+mc
else
if ma+mb+mc>0 then play =1
end if
until play=1 or n=1000

# in this case the computer is loosing
if play = 0 then

letter = int(int(rand*22)/10)

if a=0 and letter = 0 then letter = 1
if b=0 and letter = 1 then letter = 2
if c=0 and letter = 2 then letter = 0
if a=0 and letter = 0 then letter = 1
if b=0 and letter = 1 then letter = 2

if letter = 0 then a= a-int(rand*a*.7+1)
if letter = 1 then b= b-int(rand*b*.7+1)
if letter = 2 then c= c-int(rand*c*.7+1)
end if

return


test:
sum=0
a1=a : b1=b :c1=c
for y=2 to 0 step -1
fa=0 : fb=0 : fc=0
if a>=2^y then
a=a-2^y
fa=1
end if
if b>=2^y then
b=b-2^y
fb=1
end if
if c>=2^y then
c=c-2^y
fc=1
end if
if fa=0 xor fb=0 xor fc=0 then
sum= sum+0
else
sum=sum+1
end if
next y
a=a1 : b=b1 : c=c1
return

draw:
clg
text 33,20," A          B          C"
for n = 1 to 10
if a>= n then circle 50,n*30+30,10
if b>= n then circle 150,n*30+30,10
if c>= n then circle 250,n*30+30,10
next n
print "a "+a
print "b "+b
print "c "+c
return

Friday, November 19, 2010

Christmas


fastgraphics
dim x$(62,62)
For y = 1 to 60
For x = 1 to 60
if int(rand*50)=5 then x$[x,y]="*"
next x
next y
For pass = 1 to 3
For n = 1 to 300
clg
color black
rect 0,0,300,300
color white
rect 0,250,300,50
circle 0,300,100
circle 200,300,80
circle 100,245,10
circle 100,230,5
circle 250,50,15
For y = 1 to 60
For x = 1 to 60
text x*5,n+y*5, x$[x,y]
if n+y*5>300 then text x*5,n+y*5-300, x$[x,y]
next x
next y
Text n,130," ^           '' "
Text n,135," 0_ ....../> "
Text n,145,"( )_, "
Text n,150,"____/ / \"
refresh
next n
next pass

Thursday, November 18, 2010

Fibonacci sequence

one=1
two=1
For n = 1 to 100
Print one
print two
one=one + two
two=two + one
next n

Roman number converter

for rm = 1 to 3000
print rm+" = ";
n=rm
for t = 1 to 3
if n-1000>=0 then
n=n-1000
print "M";
end if
next t
if n-900>=0 then
n=n-900
print "CM";
end if
if n-500>=0 then
n=n-500
print "D";
end if
if n-400>=0 then
n=n-400
print "CD";
end if
for f = 1 to 3
if n-100>=0 then
n=n-100
print "C";
end if
next f
if n-90>=0 then
n=n-90
print "XC";
end if
if n-50>=0 then
n=n-50
print "L";
end if
if n-40>=0 then
n=n-40
print "XL";
end if
for h = 1 to 3
if n-10>=0 then
n=n-10
print "X";
end if
next h
if n-9>=0 then
n=n-9
print "IX";
end if
if n-5>=0 then
n=n-5
print "V";
end if
if n-4>=0 then
n=n-4
print "IV";
end if
for k = 1 to 3
if n-1>=0 then
n=n-1
print "I";
end if
next k
print
next rm

Wednesday, November 17, 2010

Probability paired events

clg
graphsize 700,300
font "arial", 60,50
text 0,180,"<" :text 70,210,"<" : text 70,150,"<"
font "arial", 12,100
r1=0 : r2=0 : b1=0 : b2=0 : f=1.2
color yellow
rect 0,80,50,50 : rect 70,80,50,50
For y=1 to 4
For x=1 to 4
box1red=int(rand*f): box2red=int(rand*f)
box1blue=int(rand*(f-box1red)) : box2blue=int(rand*(f-box2red))
r1=r1+box1red : r2=r2+box2red
b1=b1+box1blue : b2=b2+box2blue
color red
if box1red=1 then circle x*10,80+y*10,3
if box2red=1 then circle 70+x*10,80+y*10,3
color blue
if box1blue=1 then circle x*10,80+y*10,3
if box2blue=1 then circle 70+x*10,80+y*10,3
next x
next y
color black
text 120,160,"P(RR)" :text 120,200,"P(RB)"
text 120,220,"P(BR)" : text 120,260,"P(BB)"
input "Redraw a ball at random from each box. What is the probability of each combination",a$
prr=(r1*r2)/((r1+b1)*(r2+b2)) : prb=(r1*b2)/((r1+b1)*(r2+b2))
pbr=(b1*r2)/((r1+b1)*(r2+b2)) : pbb=(b1*b2)/((r1+b1)*(r2+b2))
trr=0:trb=0:tbr=0:tbb=0
for n = 1 to 1000
draw=rand
if draw < prr then
trr=trr+1
print "RR"
end if
if prr < draw and draw < prr+prb then
trb=trb+1
Print "RB"
end if
if prr+prb < draw and draw < prr+prb+pbr then
tbr=tbr+1
Print "BR"
end if
if prr+prb+pbr < draw and draw < prr+prb+pbr+pbb then
tbb=tbb+1
Print "BB"
end if
rect 170,170,trr,5
rect 170,210,trb,5
rect 170,230,tbr,5
rect 170,270,tbb,5
next n
Print "Theorical probability"
print "P(RR)="+prr+"="+(r1*r2)+"/"+((r1+b1)*(r2+b2))
print "P(RB)="+prb+"="+(r1*b2)+"/"+((r1+b1)*(r2+b2))
print "P(BR)="+pbr+"="+(b1*r2)+"/"+((r1+b1)*(r2+b2))
print "P(BB)="+pbb+"="+(b1*b2)+"/"+((r1+b1)*(r2+b2))
Print "Experimental results for "+(n-1)+" trials"
print "P(RR)="+(trr/(n-1))+"="+trr+"/"+(n-1)
print "P(RB)="+(trb/(n-1))+"="+trb+"/"+(n-1)
print "P(BR)="+(tbr/(n-1))+"="+tbr+"/"+(n-1)
print "P(BB)="+(tbb/(n-1))+"="+tbb+"/"+(n-1)

Friday, November 12, 2010

Chaos Game

fastgraphics
rx=300:ry=150:x=150:y=0
for sides = 3 to 10
for m = 1 to 10
mult=m/10
for t = 0 to 200
refresh
gosub plotthepoints
next t
clg
next m
next sides
choosevertex:
angle = (360/sides)*int(rand*sides*360)
rad = -3.14159*(angle/180)
y=(sin (rad))*150
x=(cos (rad))*150
return
plotthepoints:
for cicle = 1 to 200
gosub choosevertex
midx= (x-rx)*mult+rx
midy= (y-ry)*mult+ry
plot 150+midx,150+midy
rx=midx
ry=midy
next cicle
return

Saturday, November 6, 2010

Decimal to binary

clg
font "arial",10,100
input "Number to change to binary up to 1024 ",a
n=10
dim b(n+1)
do
if a-2^n>-1 then
b[n]=1
a=a-2^n
end if
n=n-1
until n<0
for x = 0 to 10
text 290-x*25+10,10,b[x]
text 290-x*25+10,30,b[x]*2^x
next x

Thursday, November 4, 2010

Orbit


rem parameters are for a geostationary orbit arround earth
font "arial",10,100
height = 36000000
speed=3070
radius=6371000
time=0 : scale=400000 :dt=40 :g=9.81
x=0 : y=radius+height
px=0:py=0
xspeed=speed : yspeed=0
fastgraphics
loop:
dist=((x-px)^2 + (y-py)^2)^(1/2)
gravity= g*(radius/dist)^2
dx=x-px : dy=y-py
xac= (dx/dist)*gravity : yac= (dy/dist)*gravity
xspeed=xspeed-xac*dt : yspeed=yspeed-yac*dt
x=x+xspeed*dt : y=y+yspeed*dt
clg
Text 150 + x/scale,150 + y/scale,"[]o[]"
circle 150+px,150+py,radius/scale
time=time+dt
speed = int(((xspeed)^2 + (yspeed)^2)^(1/2))
text 10,10,"height = "+dist/radius+"radius"
text 10,30,"speed = "+speed+"m/s"
text 10,50,"time = "+int(time/3600)+"h"
text 10,70,"grav-acc = "+(radius/dist)^2+"g"
text 10,90,"centri-acc = "+(speed^2/dist)/g+"g"
refresh
goto loop

Monday, November 1, 2010

Game of life


Rem http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
fastgraphics
font "Arial", 20,100
graphsize 600,600
dim cell1(101,101)
dim cell2(101,101)
For x = 2 to 99
For y = 2 to 99
cell1[x,y]=int (rand*2)
next y
next x
for turn = 1 to 1000
For x = 2 to 99
For y = 2 to 99
neighbours = cell1[x-1,y]+cell1[x-1,y-1]+cell1[x,y-1]+cell1[x-1,y+1]+cell1[x+1,y-1]+cell1[x+1,y+1]+cell1[x,y+1]+cell1[x+1,y]
cell2[x,y]=cell1[x,y]
if cell1[x,y]=1 and (neighbours<2 or neighbours>3 ) then cell2[x,y]=0
if cell1[x,y]=0 and neighbours=3 then cell2[x,y]=1
next y
next x
For x = 2 to 99
For y = 2 to 99
cell1[x,y]=cell2[x,y]
color rgb( 255*(1-cell1[x,y]),255*(1-cell1[x,y]),255*(1-cell1[x,y]))
rect (6*x,6*y,6,6)
next y
next x
color red
text 10,10,"Cicle"+turn
refresh
next turn

Wave Interference


fastgraphics
clg
rem wave source coordetanes
x1=150 : y1= 300 : x2= 0 : y2=0
for n = 1 to 100 step .2
For x = 0 to 300 step 5
For y = 0 to 300 step 5
d1 = ((x-x1)^2+(y-y1)^2)^0.5
d2 = ((x-x2)^2+(y-y2)^2)^0.5
s1=60*sin(d1/5-n)
s2=60*sin(d2/5-n)
if 5*n>=d2 then
color rgb(122+s2,122+s2,122+s2)
else
color rgb(122+s1,122+s1,122+s1)
end if
if 5*n>=d1 and 5*n>=d2 then color rgb( 122+s1+s2, 122+s1+s2,122+s1+s2)
if 5*n<=d1 and 5*n<=d2 then color grey
circle x,y,3
next y
next x
refresh
next n