The 20line or less weekly contest!
Author |
Message |
ClayWall
|
Posted: Tue Nov 22, 2011 12:34 am Post subject: Re: The 20line or less weekly contest! |
|
|
Nothing fancy just thought if I'm the only entry this week I might win!
code: |
var number : int
var key : string (1)
loop
locate (10, 1)
for x : 0 .. 255
color (x)
put x : 5 ..
end for
locate (1, 1)
put "Enter Number And Press Enter (0,255)"
get number
if number >= 0 and number <= 255 then
Draw.FillBox (10, 260, 110, 360, number)
put "Press Any Button To Continue" ..
else
put "Number Outside Of Range" ..
end if
getch (key)
Text.Cls
end loop
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: Wed Nov 23, 2011 10:35 am Post subject: RE:The 20line or less weekly contest! |
|
|
Denied.
Turing: |
fcn moddedFcn (f : fcn f2 (x : real) : real, x, transX, transY, scaleX, scaleY : real) : real
handler (e )
result 0
end handler
result scaleY * f (x * scaleX - transX ) + transY
end moddedFcn
proc drawFcn (originX, originY : real, f : fcn f2 (x : real) : real, transX, transY, scaleX, scaleY : real)
for x : floor (-originX ) .. ceil (maxx - originX )
var y1 := round (originY + moddedFcn (f, x, transX, transY, scaleX, scaleY ))
var y2 := round (originY + moddedFcn (f, x + 1, transX, transY, scaleX, scaleY ))
Draw.Line (round (originX + x + 1), 0, round (originX + x + 1), y2, gray) % Fill
Draw.Line (round (originX + x ), y1, round (originX + x + 1), y2, black)
end for
Draw.Line (round (originX ), 0, round (originX ), maxy, red)
Draw.Line (0, round (originY ), maxx, round (originY ), red)
end drawFcn
fcn f (x : real) : real
result 1 / sin (x ) + x ** 3 % Edit Me!
end f
drawFcn (maxx / 2, maxy / 2, f, 0, 0, 1 / 50, 1)
|
Figured since this is no longer active, we should do a slightly bigger timeframe contest. I'm thinking along the lines of a monthly thing. The rules 'd change from month to month though. |
|
|
|
|
|
Dreadnought
|
Posted: Thu Nov 24, 2011 7:11 pm Post subject: Re: The 20line or less weekly contest! |
|
|
Well, I don't know if this program is contest material but I figured I post it anyway.
It efficiently computes the nth fibonacci number... almost. For values of n larger than 74 it loses the last digits because the real numbers in turing only have 16 digit precision. (It is currently set to show the 1200th one)
Here it is (I know I've used some horrible coding practices, but its 20 lines)
Turing: |
var a, b, c : array 0 .. 1 of real := init (0. 5, 0. 5)
fcn mult_qfe (x, y : array 0 .. 1 of real) : array 0 .. 1 of real
c (0) := x (0) * y (0) + x (1) * y (1) * 5
c (1) := x (1) * y (0) + x (0) * y (1)
result c
end mult_qfe
fcn exp_qfe (x : array 0 .. 1 of real, n : int) : array 0 .. 1 of real
if n = 1 then
result x
else
if n mod 2 = 0 then
result mult_qfe (exp_qfe (x, n div 2), exp_qfe (x, n div 2))
end if
result mult_qfe (x, mult_qfe (exp_qfe (x, n div 2), exp_qfe (x, n div 2)))
end if
end exp_qfe
b (1) * = - 1
a := exp_qfe (a, 1200) % Edit the numerical values for this line and the next
b := exp_qfe (b, 1200) % Make sure they are identical
put frealstr (a (1) - b (1), 16, 1)
|
You can't go much higher than the 1200th before getting a real expression overflow, but the fact that you only have the first 16 digits (they're accurate) and 3 lines of zeros is unfortunate.
Its light-years faster than the conventional recursive method of adding all numbers in the sequence up to the desired one (the 50th would probably take over an hour if you don't overflow). |
|
|
|
|
|
Insectoid
|
Posted: Thu Nov 24, 2011 7:55 pm Post subject: RE:The 20line or less weekly contest! |
|
|
Your mult_qfe function can probably be a procedure, and you can remove the 'result c' line completely since c is of global scope. What you're doing right now is modifying C, then setting C equal to itself.
Then again, I have no idea what your code is doing (if you'd mind re-posting it with comments and proper function/variable names, that'd be great) so you might actually need it. Looking closer at your code you might actually be saving lines this way by not having to explicitly return C. |
|
|
|
|
|
Dreadnought
|
Posted: Fri Nov 25, 2011 12:00 am Post subject: Re: The 20line or less weekly contest! |
|
|
I thought of making it a procedure, but each procedure call is one line and in this case lines are precious so I left it as a function. As for the usage of the global C, its purpose was just to save a line of code within the mult_qfe function which would have declared C locally.
I agree, though that the code is though to read. It was closer to 30 lines when it was readable so removing lines meant creating gibberish.
To understand what's going on, I'm using a closed form to generate numbers in the Fibonacci sequence very quickly. The equation for the nth number in the sequence is
Fn = (q^n - (1 - q)^n)/(sqrt 5), where q = (1 + (sqrt 5))/2
Since you asked, here's a readable, commented version of my code (I changed QFE to IRE)
Turing: |
type IRE : % A IRE (Irraitonal Root Equation) is an equation of the form (R + I * sqrt(5))
record % In the short version I use an array where IRE(0) = R and IRE(1) = I
R : real % R is the rational part
I : real % I is the coefficient of the irrational root
end record
% Consumes two IREs, and produces their product
fcn mult_IRE (x, y : IRE ) : IRE
var product : IRE % This would have been C
product.R := x.R * y.R + x.I * y.I * 5
product.I := x.I * y.R + x.R * y.I
result product
end mult_IRE
% Recursive exponentiation of a IRE to the power n
fcn exp_IRE (x : IRE, n : int) : IRE
if n = 1 then % Base case: n = 1
result x
else
y := exp_IRE (x, n div 2) % y = x^(n/2) OR y = x^((n-1)/2) respectively n is even OR n is odd
if n mod 2 = 0 then % n is even
result mult_IRE (y, y ) % x^n = y^2
end if
% n is odd
result mult_IRE (x, mult_IRE (y, y )) % x^n = x* y^2
end if
end exp_IRE
var a, b : IRE
a.R := 0. 5 % a = (1 + sqrt(5))/2
a.I := 0. 5
b.R := 0. 5 % b = (1 - sqrt(5))/2 also (1 - a)
b.I := - 0. 5
var n : int := 1200
% Raise both a and b to the power of n
var a_power_n := exp_IRE (a, n )
var b_power_n := exp_IRE (b, n )
% (a^n - b^n)/sqrt(5)
% Simplifies to (a.I - b.I)
put frealstr (a_power_n.I - b_power_n.I, 16, 1)
|
By the way, IRE and QFE were just made up names for an equation with an irrational root. I use them to maintain complete accuracy throughout computation.
Also, on line 10 of my original code, I could use elsif and save 2 lines, derp. |
|
|
|
|
|
Aange10
|
Posted: Fri Nov 25, 2011 12:24 am Post subject: RE:The 20line or less weekly contest! |
|
|
y has not been declared. And as for a color tester
Turing: |
var color1 : int := 2
var chars : array char of boolean
loop
cls
put "Press 1 to add a number to color \nPress 2 to subtract a number from a color \ncolor: " + intstr (color1 )
drawfilloval (maxx div 2, maxy div 2, 65, 65, grey)
drawfilloval (maxx div 2 - 230, maxy div 2, 50, 50, color1 - 2)
drawfilloval (maxx div 2 - 115, maxy div 2, 50, 50, color1 - 1)
drawfilloval (maxx div 2, maxy div 2, 50, 50, color1 )
drawfilloval (maxx div 2 + 115, maxy div 2, 50, 50, color1 + 1)
drawfilloval (maxx div 2 + 230, maxy div 2, 50, 50, color1 + 2)
delay (100)
Input.KeyDown (chars )
if chars ('1') and color1 < 253 then
color1 + = 1
elsif chars ('2') and color1 > 2 then
color1 + = - 1
end if
end loop
|
use it all the time
EDIT: Or if we are aloud to use semi colens
Turing: |
var color1 : int := 2
var chars : array char of boolean
loop
cls
put "Press 1 to add a number to color \nPress 2 to subtract a number from a color \ncolor: " + intstr (color1 )
drawfilloval (maxx div 2, maxy div 2, 65, 65, grey);drawfilloval (maxx div 2 - 230, maxy div 2, 50, 50, color1 - 2);drawfilloval (maxx div 2 - 115, maxy div 2, 50, 50, color1 - 1);drawfilloval (maxx div 2, maxy div 2, 50, 50, color1 );drawfilloval (maxx div 2 + 115, maxy div 2, 50, 50, color1 + 1);drawfilloval (maxx div 2 + 230, maxy div 2, 50, 50, color1 + 2);Draw.ThickLine (maxx div 2 + 285, maxy div 2, maxx div 2 + 295, maxy div 2, 3, black); Draw.ThickLine (maxx div 2 + 295, maxy div 2, maxx div 2 + 285, maxy div 2 + 10, 3, black); Draw.ThickLine (maxx div 2 + 295, maxy div 2, maxx div 2 + 285, maxy div 2 - 10, 3, black); Draw.ThickLine (maxx div 2 - 285, maxy div 2, maxx div 2 - 295, maxy div 2, 3, black);Draw.ThickLine (maxx div 2 - 295, maxy div 2, maxx div 2 - 285, maxy div 2 + 10, 3, black); Draw.ThickLine (maxx div 2 - 295, maxy div 2, maxx div 2 - 285, maxy div 2 - 10, 3, black)
delay (100)
Input.KeyDown (chars )
if chars ('1') and color1 < 253 then
color1 + = 1
elsif chars ('2') and color1 > 2 then
color1 + = - 1
end if
end loop
|
|
|
|
|
|
|
Dreadnought
|
Posted: Fri Nov 25, 2011 12:50 pm Post subject: Re: The 20line or less weekly contest! |
|
|
Ya, your right, line 20 should be
var y : IRE := exp_IRE (x, n div 2)
Change that and it should work. (the 20 line version still works though) |
|
|
|
|
|
Jeffmagma
|
Posted: Thu Dec 10, 2015 1:21 pm Post subject: Re: The 20line or less weekly contest! |
|
|
Doesn't seem like this is going on anymore, but for fun:
Turing: |
var x1, x2, y1, y2, b : int := 0
loop
x2 := x1
y2 := y1
mousewhere (x1, y1, b )
if b = 1 then
Draw.ThickLine (x1, y1, x2, y2, 5, brightred)
end if
end loop
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Jeffmagma
|
Posted: Fri Jan 22, 2016 1:49 pm Post subject: Re: The 20line or less weekly contest! |
|
|
yay boxes
Turing: | var mx, my, mb, ml : int := 0
var sx, sy : int
var back : int := Pic.New (0, 0, maxx, maxy)
setscreen ("offscreenonly")
loop
mousewhere (mx, my, mb )
Pic.Draw (back, 0, 0, picCopy)
if ml = 0 and mb = 1 then
sx := mx
sy := my
elsif mb = 1 then
drawbox (mx, my, sx, sy, black)
end if
View.Update
if ml = 1 and mb = 0 then
drawbox (mx, my, sx, sy, black)
back := Pic.New (0, 0, maxx, maxy)
end if
ml := mb
end loop
|
|
|
|
|
|
|
|
|