## Friday, December 31, 2010

### Caesar's cipher decryption

ciphr\$="ňŭğŢűŸůųŮŦűŠůŧŸīğŠğłŠŤŲŠűğŢŨůŧŤűīğŠūŲŮğŪŭŮŶŭğŠŲğŠğłŠŤŲŠűĦŲğŢŨůŧŤűīğųŧŤğŲŧŨťųğŢŨůŧŤűīğłŠŤŲŠűĦŲğŢŮţŤğŮűğłŠŤŲŠűğŲŧŨťųīğŨŲğŮŭŤğŮťğųŧŤğŲŨŬůūŤŲųğŠŭţğŬŮŲųğŶŨţŤūŸğŪŭŮŶŭğŤŭŢűŸůųŨŮŭğųŤŢŧŭŨŰŴŤŲħŖŨŪŨůŤţŨŠĨ"

text\$=""
shift=255
for n = 1 to length(ciphr\$)
letter\$= mid(ciphr\$, n, 1)
m = asc(letter\$)
text\$=text\$+chr(m-shift)
next n
print text\$

## Thursday, December 30, 2010

### Caesar's cipher encryption

text\$="In cryptography, a Caesar cipher, also known as a Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques(Wikipedia)"
shift=255
ciphr\$=""
for n = 1 to length(text\$)
letter\$= mid(text\$, n, 1)
m = asc(letter\$)
ciphr\$=ciphr\$+chr(m+shift)
next n
print ciphr\$

### Sphere

clg
For n = 1 to 150
color rgb(50+n,50+n,50+n)
circle rgb(150,150,150-n)
next n

### Smarties

clg
ns=int(rand*500)
For n = 1 to ns
color int(rand*2)*255,int(rand*2)*255,int(rand*2)*255
circle rand*290+5,rand*290+5,5
next n
input "How many smarties?",wait
print ns

## Wednesday, December 29, 2010

### Loan payment plan

# Loan payment plan ( fixed monthly)
# r ( anual interest rate 3.4% )
# l amount borrowed
# n duration in moths
r=0.034
l=180000
n=360
c=r/12
p=l*(c*(1+c)^n)/((1+c)^n-1)
amort = 0
tota=0
toti=0
for m = 1 to n
l=l-amort
inter = l*c
amort=p-inter
tota=tota+amort
toti=toti+inter
prest=inter+amort
print "Month ="+m+" Amort.="+int(amort*100)/100+" Intrest="+int(inter*100)/100+" Total="+int(prest*100)/100
next m
print
print "Monthly payment = "+p
Print "Total amortization = "+tota

Print "Total interest = "+toti

### Dodge Ball

# Use "a" and "d" to turn
# Avoid crashing the arrow
font "arial",20,100
fastgraphics
graphsize 600,600
dim a(6)
a = {3,0,5,10,0,10}
px=50
py=550
x=550
y=50
turn =0
xspeed1=0
yspeed1=0
r=5

for t = 1 to 100000
dist=((x-px)^2 + (y-py)^2)^(1/2)
xspeed1=xspeed1-(x-px)*.01/abs(x-px)
yspeed1=yspeed1-(y-py)*.01/abs(y-py)
x=x+xspeed1 : y=y+yspeed1
z = key
a\$="G"
if z = 65 then a\$="L"
if z = 68 then a\$="R"
if a\$="L" and turn>-.2 then
turn =turn-.05
else
turn=turn*.95
end if
if a\$="R" and turn<.2  then turn =turn+.05
py = py + yspeed
px = px + xspeed
pause .01
clg
color yellow
rect 0,0,600,600
color black
text 100,10,"Points= "+t
circle x,y,r
refresh
if px<0 or px > 600 or py < 0 or py > 600 or dist < r then
text 10,10,"Lost"
refresh
end
end if
if x<0 or x>600  then
xspeed1=-xspeed1
r=r+5
end if
if y < 0 or y > 600 then
yspeed1=-yspeed1
r=r+5
end if
next t

## Saturday, December 25, 2010

### Mandelbrot

rem Zoomable Mandelbrot based in Joel Kahn's example
sz=600
font "arial",10,100
graphsize sz,sz
fastgraphics
kt=50:m=4.0
xmin=-2.1:xmax=.6:ymin=-1.5:ymax=1.5
gosub draw
input "press enter to zoom in",a\$
xmin=-0.5:xmax=0:ymin=0.5:ymax=1
gosub draw
input "press enter to zoom in",a\$
xmin=-0.15:xmax=-.05:ymin=0.9:ymax=1
gosub draw
input "press enter to zoom in ",a\$
xmin=-0.14:xmax=-.13:ymin=0.98:ymax=.99
gosub draw
end

draw:
clg
refresh
dx=(xmax-xmin)/sz:dy=(ymax-ymin)/sz
for x=xmin to xmax step dx
for y=ymin to ymax step dy
k=0:a=0:b=0
do
# In the iteration z=z^2+c
#z=ai + b
#c=yi + x
tx=a^2-b^2+x
b=2*b*a+y
a=tx
d=a^2+b^2
k=k+1
until not (d <= m and k < kt)
color k*50000000
plot (x-xmin)/dx,(ymax-y)/dy
next y
refresh
next x
# grid
color black
for n= 0 to 9
line n*sz/10,sz,n*sz/10,0
text n*sz/10,5,xmin+(xmax-xmin)*n/10
text 5,n*sz/10,ymax-(ymax-ymin)*n/10
line sz,n*sz/10,0,n*sz/10
next n
refresh
return

## Wednesday, December 22, 2010

### Bifurcation Diagram

# http://en.wikipedia.org/wiki/Bifurcation_diagram

Graphsize 600,600
clg
fastgraphics
for c = 0 to 2 step 0.01
refresh
y=0
# Main sequence
for i=1 to 20
print "c="+c+" i="+i+" y="+y
y = y^2-c
circle 200*c,100*y+300,1
next i
next c
# graph c is the horizontal axis
for c = 0 to 600 step 40
for y = -300 to 300 step 50
text c,7,c/200
text 0,300-y,y/100
line c,0,c,600
line 0,y+300,600,y+300
next y
next c
refresh
end

## Tuesday, December 21, 2010

### Ramdom Walk

Rem http://en.wikipedia.org/wiki/Random_walk

clg
font "arial",20,100
graphsize 600,450
fastgraphics
stepsize=10
steps=0
For radius = 20 to 200 step 10
average=steps/10
print " Average number of steps="+average
steps=0
for trial = 1 to 10
x=0
y=0
color grey
rect 0,0,600,40
color yellow
color black
text 10,10,"Trial"+trial
steps = steps+1
a=rand*2*pi
dx=cos(a)*stepsize
dy=sin(a)*stepsize
line 200+x,200+y,200+x+dx,200+y+dy
circle x+200,y+200,1
refresh
x=x+dx
y=y+dy
end while
next trial

## Sunday, December 19, 2010

### 3D plotter

fastgraphics
clg : font "arial",15,100
graphsize 600,600
zoom=5 : max=5 : point=0
for z = -max to max step .1
for y = -max to max step .1
for x = -max to max step .1
Rem change this bit
if (x^2+y^2<1+z^2)then

if abs(x)>(max-.1) or abs(y)>(max-.1) or abs(z)>(max-.1) then rc=0
color rgb( 100+x*(100/max)+rand*rc,100+y*(100/max)+rand*rc,100+z*(100/max)+rand*rc)
point=point+1
gosub graph
else
if (abs(z)<0.01 and abs(y)<0.01) or (abs(x)<0.01 and abs(y)<0.01) or (abs(x)<0.01 and abs(z)<0.01) then
color black
if z>max-.1 then text sx+5,sy-10,"z="+max
if x>max-.1 then text sx+5,sy-10,"x="+max
if y>max-.1 then text sx+5,sy-10,"y="+max
refresh
gosub graph
end if
end if
next x
next y
refresh
next z
print "Aproximate volume ="+point/1000
graph:
prespective=1+(-3*max+x+y+z)/50
sx = 300+ (x*7-y*7)*zoom*prespective
sy = 300+ (-z*7+y*3+x*3)*zoom*prespective
circle sx,sy,2
rc=50
return

## Saturday, December 18, 2010

### Lorenz attractor

rem http://en.wikipedia.org/wiki/Lorenz_attractor
clg
fastgraphics
graphsize 900,300
s=10
p=28
b=8/3
x=10
y=0
z=10
zoom=6
dt=0.002
for n = 1 to 100000
dx=s*(y-x)
dy=x*(p-z)-y
dz=x*y-b*z
x=x+dx*dt
y=y+dy*dt
z=z+dz*dt
plot 150+zoom*y,300-zoom*z
plot 400+zoom*x,300-zoom*z
plot 650+zoom*x,150-zoom*y
refresh
next n

## Wednesday, December 15, 2010

### Rule 30

Rem http://en.wikipedia.org/wiki/Rule_30
clg
fastgraphics
graphsize 900,450
plot (450,1)
print pixel (10,10)
For y = 1 to 450
For x = 1 to 900
if pixel (x-1,y-1)=0 and pixel (x,y-1)<>0 and pixel (x+1,y-1)<>0 then plot(x,y)
if pixel (x-1,y-1)<>0 and pixel (x,y-1)=0 and pixel (x+1,y-1)=0 then plot(x,y)
if pixel (x-1,y-1)<>0 and pixel (x,y-1)=0 and pixel (x+1,y-1)<>0 then plot(x,y)
if pixel (x-1,y-1)<>0 and pixel (x,y-1)<>0 and pixel (x+1,y-1)=0 then plot(x,y)
next x
refresh
next y

### 100!

Rem Find the sum of the digits in the number 100! (Project Euler 20)
p=160
graphsize 1200,5
Dim Prod(p+1)
Prod=1
for b = 1 to 100
for t = 0 to p-1
Prod[t]=Prod[t]*b
next t
Rem Carying
for x = 0 to p-2
While Prod[x]>9
Prod[x]=Prod[x]-10
Prod[x+1]=Prod[x+1]+1
end While
next x
Rem Printing
Print b+"! = ";
for x = 0 to p
Print Prod[p-x];
next x
print
next b
sum=0
for x = 0 to p-1
sum = sum + Prod[x]
next x
print sum

## Tuesday, December 14, 2010

### 2^301

Rem 2^301 calculation

p=100
graphsize 1000,5
Dim Prod(p+1)
Prod=1

for b = 0 to 300
for t = 0 to p-1
Prod[t]=Prod[t]*2
next t

Rem Carying
for x = 0 to p-2
While Prod[x]>9
Prod[x]=Prod[x]-10
Prod[x+1]=Prod[x+1]+1
end While
next x

Rem Printing
Print "2^"+(b+1)+" = ";
for x = 0 to p
Print Prod[p-x];
next x
print
next b

## Monday, December 13, 2010

### "Infinite" precision

Rem Infite multiplication precision algorithm http://en.wikipedia.org/wiki/Infinite_precision_arithmetic
max=10
p=2*max+2
graphsize 600,600
font "arial", 8,100
Dim n1(max+1)
Dim n2(max+1)
Dim Prod(p)
Rem {units ,tens ,hundreds ,etc }
n1={2,9,7,5,6,4,3,4,3,4}
n2={9,9,3,4,5,2,4,3,4,3}

Rem Multiplication
for y = 0 to max
for x = 0 to max
Prod[x+y]=Prod[x+y]+n1[x]*n2[y]
next x
next y

Rem Carying
for x = 0 to p-2
While Prod[x]>9
Prod[x]=Prod[x]-10
Prod[x+1]=Prod[x+1]+1
end While
next x

Rem Printing
for x = 0 to max
print n1[max-x];
next x
Print" x ";
for x = 0 to max
print n2[max-x];
next x
Print" = ";
p=p-1
for x = 0 to p
Print Prod[p-x];
next x

## Wednesday, December 8, 2010

### Euler 10

Rem Find the sum of all the primes below two million (Project Euler 10)
flag=0
sum=2
print 2+"....Sum="+sum
for h= 2 to 2000000
flag=0
i=1
while flag=0 and i < h^(1/2)
i=i+1
if int(h/i)=h/i then flag=1
end while
if flag=0 then
sum=sum+h
print (h)+"....Sum="+sum
end if
next h

## Sunday, December 5, 2010

### Black hole

graphsize 600,600
x=160
y=180
xspeed=2
yspeed=-1
r=2
n=400
dim x(n+1)
dim y(n+1)
dim xspeed(n+1)
dim yspeed(n+1)
dim dist(n+1)
dim dx(n+1)
dim dy(n+1)
dim xac(n+1)
dim yac(n+1)
dim gravity(n+1)
dim crash(n+1)
colision = 0
for p = 1 to n
x[p]=rand*600
y[p]=rand*600
next p
for s = 1 to n
xspeed[s]=rand*2-1
yspeed[s]=rand*2-1
next s
px=300
py=300
circle px,py,5
fastgraphics
loop:
clg

for t = 1 to n
dist[t]=((x[t]-px)^2 + (y[t]-py)^2)^(1/2)
gravity[t] = (r/dist[t])^2
dx[t]=x[t]-px
dy[t]=y[t]-py
xac[t]= (dx[t]/dist[t])*gravity[t]
yac[t]= (dy[t]/dist[t])*gravity[t]
if crash[t]=0 then xspeed[t]=xspeed[t]*.999-xac[t]
if crash[t]=0 then yspeed[t]=yspeed[t]*.999-yac[t]
if crash[t]=0 then x[t]=x[t]+xspeed[t]
if crash[t]=0 then y[t]=y[t]+yspeed[t]
if dist[t] < r and crash[t]=0 then
r=r+8/r^2
colision = colision+1
crash[t]=1
end if
text 10,10,"Colisions="+colision
circle x[t],y[t],2
next t
circle px,py,r
refresh
goto loop

## Friday, December 3, 2010

### Forest fire

fastgraphics
graphsize 600,600
font "Arial", 20,100
dim cell(101,101)
cell[50,50]=2
burned = 1
Rem probabilitity to catch fire
p=.3
for turn = 1 to 1000
For x = 2 to 99
For y = 2 to 99
if cell[x-1,y]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x,y-1]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x,y+1]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x+1,y]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x-1,y-1]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x-1,y+1]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x+1,y-1]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x+1,y+1]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x,y]=3 then burned = burned + 1
next y
next x
For x = 2 to 99
For y = 2 to 99
if cell[x,y]=0 then color green
if cell[x,y]=1 then color darkorange
if cell[x,y]=2 then color red
if cell[x,y]=3 then color yellow
if cell[x,y]>1 then cell[x,y]=cell[x,y]-1
rect 6*x,6*y,6,6
next y
next x
color blue
Text 10,10,"Cicle="+turn+" % burned trees=" +(burned/100)
refresh
next turn

### Dragon curve

graphsize 900,900
color yellow
rect 0,0,900,900
color black
fastgraphics
order=15
Dim s(2^order+1)
s=1 : s=1
for n=2 to order
a= 2^n-1 : b=2^(n-1)+1
for g= a to b step -1
s[g]= abs(s[2^n-g]-1)
next g
s[2^n]=1
next n
Zoom =3 : angle=180
x1=110*Zoom : y1=90*Zoom
for g= 1 to 2^(order)-1
refresh
gosub lines
if s[g]=1 then angle =angle+90
if s[g]=0 then angle =angle-90
next g
lines:
r = (angle/180)*pi
y2=y1 + (sin (r))*Zoom
x2=x1 - (cos (r))*Zoom
line x1,y1,x2,y2
x1=x2
y1=y2
return