Computer Science Canada

The 20line or less weekly contest!

Author:  MihaiG [ Thu Feb 26, 2009 1:54 am ]
Post subject:  The 20line or less weekly contest!

Update:
Since this started thursday, the submitted code will be reviewed wednesday night(or thursday morning) based on submission time(server time), Users and mods can both participate(cept mods dont get bits) winners will recieve 100bits and a "virtual" trophy/badge for each week.


Hey guys,

ive really gotten into having fun in optimizing code so i want to start a weekly event, where users submit programs written in 20 lines or less.
Users will be encouraged to be creative and improve on the code and ideas of others. Some basic rules i want to lay out are:

1.Submissions must have a total length(20 lines or less, commenting does not count).
2.Code must be commented(where not obvious, assume we have only programmed for 3 weeks)
3.Ideas and code may be used from previous submissions in the case where the resulting code is at least 3 lines shorter than the previous(and or similar) version.
4. Reasonable explanations must be included with each submission.
5. Prizes of 100bit will be awarded to weekly winners Smile

And to start it off, i will warp your minds with the Sierpinski Triangle.

code:

View.Set ("graphics:512;512")
%i need a temporary value to store the AND operation to since turing doesnt like using it directly
var z : int
for x : 1 .. maxx%one iteration for each pixel
    for y : 1 .. maxy
        z := x & y
        if z = 0 then
            Draw.Dot (x, y, 7)
        end if
    end for
end for

What this essentialy does it if the AND operation between two coordinates results in zero then a black pixel is drawn,(you can also switch that z = 0 to a z != 0)
Posted Image, might have been reduced in size. Click Image to view fullscreen.

So thats my first submission i am waiting for your submissions!

*edit note you can use a max screen for my program, but it works best with square displays where the window size is a 2^n :]*[/img]

Author:  Tony [ Thu Feb 26, 2009 3:20 am ]
Post subject:  Re: The 20line or less weekly contest!

MihaiG @ Thu Feb 26, 2009 1:54 am wrote:
i need a temporary value to store the AND operation to since turing doesnt like using it directly

It might not like it, but it works.
Turing:

View.Set ("graphics:512;512")
for i : 0 .. maxx*maxy - 1
    Draw.Dot ((i mod maxx)+1, (i div maxx) + 1, ceil((((i mod maxx)+1) & ((i div maxx)+1))/maxint)*black)
end for

For comments, refer to MihaiG's program above. This does the same thing.

Though calling this "optimized" would be misleading, as it makes the solution worse, in just about every way imaginable.

Author:  The_Bean [ Thu Feb 26, 2009 11:00 pm ]
Post subject:  Re: The 20line or less weekly contest!

I propose a line length limit, not because of multiple commands on one line, but to prevent me from doing this...

This is to all those people that were trying to make those projectile games a while back.
Turing:

View.Set ("graphics:1024,400;nobuttonbar;title:Target Practise with Physics;position:center,center")
var velocity : real
var angle : real
var box:int:=Rand.Int(50,maxx)
Draw.FillBox(box-50,0,box+50,20,3)
Draw.ThickLine(0,0,20,10,5,28)
put "Enter power in m/s where 1 pixel = 1 meter (0..100): " ..
get velocity
put "Enter Angle in degrees (1..89): " ..
get angle
for x : 0 .. floor ((velocity ** 2 * sind (2 * angle)) / 9.8)
    Draw.ThickLine(x,round(((-((velocity*sind(angle))*(2*velocity*sind(angle))/9.8*.5-4.9*(.5*(2*velocity*sind(angle))/9.8)**2))/((velocity**2*sind(2*angle))/9.8*.5)**2)*(x-.5*(velocity**2*sind(2*angle))/9.8)**2+((velocity*sind(angle))*(2*velocity*sind(angle))/9.8*.5-4.9*(.5*(2*velocity*sind(angle))/9.8)**2)),x+1,round(((-((velocity*sind(angle))*(2*velocity*sind(angle))/9.8*.5-4.9*(.5*(2*velocity*sind(angle))/9.8)**2))/((velocity**2*sind(2*angle))/9.8*.5)** 2)*((x+1)-.5*(velocity**2*sind(2*angle))/9.8)**2+((velocity*sind(angle))*(2*velocity*sind(angle))/9.8*.5-4.9*(.5*(2*velocity*sind(angle))/9.8)**2)),3,7)
    delay(2)
end for
if (velocity ** 2 * sind (2 * angle)) / 9.8>=box-50 and (velocity ** 2 * sind (2 * angle)) / 9.8<=box+50 then
    put "You Hit It!"..
else
    put "You Missed!"..
end if

To anyone saying that its more than 20 lines, the Draw.ThickLine() is only actually on 1 line, it looks like more because of text wrapping.

And heres a nicer version for the commenting part.
Turing:

View.Set ("graphics:1024,400;nobuttonbar")
var velocity : real
var angle : real
put "Enter power(0..100): " ..
get velocity
put "Enter Angle(0..89): " ..
get angle
var tTime : real := (2 * velocity * sind (angle)) / 9.8  %calculate the time the projectile will be in the air
var maxDistance : real := (velocity ** 2 * sind (2 * angle)) / 9.8 %find the horizantal displacement of the projectile
var maxHeight : real := (velocity * sind (angle)) * tTime * .5 - 4.9 * (.5 * tTime) ** 2 % find the maximum height of the projectile
var porabolaA : real := (-maxHeight) / (maxDistance * .5) ** 2 %find the 'a' value in the vertex form of a probola y=a(x-d)**2+c where y=0 x=maxDistance d=half the max distance c=max height
for x : 0 .. floor (maxDistance)%go through all x values in the horizantal displacement
    Draw.Line (x, round (porabolaA * (x - .5 * maxDistance) ** 2 + maxHeight),x+1,round (porabolaA * ((x+1) - .5 * maxDistance) ** 2 + maxHeight), 7) %draw a line connecting one point to the next
    %%%In the other program i replaced all of the variables with what they equaled according to velocity and angle which was really long a repedative
    delay (2)
end for

Author:  MihaiG [ Fri Feb 27, 2009 11:16 am ]
Post subject:  RE:The 20line or less weekly contest!

Heres my take on it, line length should not be an issue if the line wraps around(i know ive written some nested stuff that is huge), just as long as you only have like "one" main fcn on that line

Author:  Clayton [ Fri Feb 27, 2009 11:32 am ]
Post subject:  RE:The 20line or less weekly contest!

What about curly braces? Wink Is:

Java:
public class Test
{
    public static void main (String[] args)
    {
        System.out.println("How many lines is this?");
    }
}


3 lines, or 7?

Author:  copthesaint [ Fri Feb 27, 2009 11:59 am ]
Post subject:  Re: The 20line or less weekly contest!

What counts as a line? ie does for statement, if statements ect.

Does this count as 2 lines?
code:
Loop
end loop
----------
for
end for
----------
if
end if
----------
Select case
end case
----------

Does This count as 1 line?
code:
else
----------

Author:  copthesaint [ Fri Feb 27, 2009 12:55 pm ]
Post subject:  Re: The 20line or less weekly contest!

This is my add on (15 lines) that I made, It's like The Draw.Fill AND it doesn't delete other colors.
The problem with this is though if you the color is in the are ANYWHERE it will change it. It has It's flaws and benefits.
Turing:

%This find Dots Equal to The 2nd color and replaces them with the 1st color
Draw.FillOval (25,25,25,25,black)%This is just to show you that it works
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%The program%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var Xvalue,Yvalue : int := 0
procedure ChangeDotColorArea (X1value : real, Y1value : real, X2value : real, Y2value : real, Color1 : int, Color2 : int)
Xvalue:= round (X1value)
Yvalue:= round (Y1value)
    for z : round (X1value * Y1value) .. round (X2value * Y2value)
    Xvalue+=1
    if Xvalue > X2value then
    Xvalue:= round (X1value)
    Yvalue+= 1
    end if
        if View.WhatDotColor (Xvalue,Yvalue) = Color2 then
            Draw.Dot (Xvalue,Yvalue,Color1)
        end if
    end for
end ChangeDotColorArea
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%End of the program%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ChangeDotColorArea (0,0,25,25,blue,black) %This is also just to show you that it works
ChangeDotColorArea (25,25,50,50,blue,black)
ChangeDotColorArea (25,0,50,25,red,black)
ChangeDotColorArea (0,25,25,50,red,black)

Author:  MihaiG [ Fri Feb 27, 2009 3:10 pm ]
Post subject:  RE:The 20line or less weekly contest!

Clayton: only turing, hence why it was posted in the turing section, any part of the code that is interpreted/excecuted (non commenting or non space) counts to the line count

Author:  SNIPERDUDE [ Mon Mar 02, 2009 9:40 pm ]
Post subject:  RE:The 20line or less weekly contest!

I'm guessing using semi-colons to shorten it is not allowed?

Author:  zylum [ Mon Mar 02, 2009 11:49 pm ]
Post subject:  RE:The 20line or less weekly contest!

Another Sierpinski triangle generator. Generates a random triangle.. This could be made a lot shorter with some trickery but I thought I'd keep it in this somewhat clean state Wink

code:
View.Set ("graphics:max;max")

var d : array 1 .. 8 of real

for i : 0 .. 3
    d (i * 2 + 1) := Rand.Int (1, maxx)
    d (i * 2 + 2) := Rand.Int (1, maxy)
end for

for : 0 .. 100000
    var r := Rand.Int (0, 2)
    d (7) := (d (7) + d (r * 2 + 1)) / 2
    d (8) := (d (8) + d (r * 2 + 2)) / 2
    Draw.Dot (floor (d (7)), floor (d (8)), black)
end for

Author:  syntax_error [ Mon Mar 02, 2009 11:51 pm ]
Post subject:  Re: RE:The 20line or less weekly contest!

SNIPERDUDE @ Mon Mar 02, 2009 9:40 pm wrote:
I'm guessing using semi-colons to shorten it is not allowed?



MihaiG wrote:

Clayton only turing, hence why it was posted in the turing section, any part of the code that is interpreted/excecuted (non commenting or non space) counts to the line count


Does turing have semi colons? I think not.

Edit: Fixed tags.

Author:  Tony [ Mon Mar 02, 2009 11:54 pm ]
Post subject:  RE:The 20line or less weekly contest!

Turing supports semi-colon separation of statements
Turing:

put "one"; put "two"

Author:  BigBear [ Tue Mar 03, 2009 4:14 pm ]
Post subject:  Re: RE:The 20line or less weekly contest!

Tony @ Mon Mar 02, 2009 11:54 pm wrote:
Turing supports semi-colon separation of statements
Turing:

put "one"; put "two"

I am pretty sure that 20 lines is after F2 formatting

Author:  MihaiG [ Tue Mar 03, 2009 5:55 pm ]
Post subject:  Re: The 20line or less weekly contest!

excellent zylum Very Happy love it id love to see the trickery Smile

i think the point is to strive in creating nifty pieces of code, not how to cheat the system,
also i think to be fair, in all respects, only post one submission a week,(unless requested, ie how i requested zylum's "alternate")

but listen it doesnt have to be graphical, i just did that as an example

also, i think it would be good to also post explanations of the code posted , ie zylum Very Happy

Author:  MihaiG [ Thu Mar 05, 2009 4:42 pm ]
Post subject:  Re: The 20line or less weekly contest!

Ok so first week is over, and the award goes to Zylum.
Posted Image, might have been reduced in size. Click Image to view fullscreen.


+100bits to zylum!

to start of week2 i give you fun Very Happy i give you 15(or 16) lines of code with trickery
CODE:

View.Set ("offscreenonly")
%points of a cube assuming a side length of 2 with the center at [0,0,0]
var xp : array 1 .. 8 of real := init (-1, -1, 1, 1, -1, 1, 1, -1)
var yp : array 1 .. 8 of real := init (-1, 1, 1, -1, -1, -1, 1, 1)
var zp : array 1 .. 8 of real := init (1, 1, 1, 1, -1, -1, -1, -1)
loop
    cls
    for i : 1 .. upper (xp)
    %rotates on the z axis and then the x axis, soem trickery was used, such as precomputed values of sin/cos (for 1/2 of a degree)
    %and to eliminate the use of temporary variables i used previously computed values and solved for their old values
   
        xp (i) := xp (i) * 0.999961923 + yp (i) * 0.0087265355
        yp (i) := yp (i) * 0.999961923 - ((xp (i) - yp (i) * 0.0087265355) / 0.999961923) * 0.0087265355
        zp (i) := zp (i) * 0.999961923 + yp (i) * 0.0087265355
        yp (i) := yp (i) * 0.999961923 - ((zp (i) - yp (i) * 0.0087265355) / 0.999961923) * 0.0087265355
        %enlarges the size by 10 times and reduces the "perspective" effect to a very minimal feel, aos shifts to the middel of the user's screen
        %the size is determined by the distance to the center, so the closer it is, the smaller, it is, (you can do the inverse and multiply by a larger constant to do the reverse
        Draw.FillOval (round (xp (i) * 10 / (zp (i) * 0.001 - 0.25) + maxx / 2), round (yp (i) * 10 / (zp (i) * 0.001 - 0.25) + maxy / 2), ceil (sqrt (xp (i) ** 2 + yp (i) ** 2 + zp (i) ** 2)) * 2,
            ceil (sqrt (xp (i) ** 2 + yp (i) ** 2 + zp (i) ** 2)) * 2, 7)
            delay(1)
    end for
    View.Update
end loop

you can remove the delay if it is going to slow

Author:  The_Bean [ Thu Mar 05, 2009 6:47 pm ]
Post subject:  Re: The 20line or less weekly contest!

Did someone say 3D?
Turing:

View.Set ("graphics:500,500,offscreenonly")
var xm, ym, bm : int
loop
    Mouse.Where (xm, ym, bm)
    cls
    for x : -18 .. 18
        for y : -18 .. 18
            Draw.FillOval(round(200*cosd(x*10)*sind(y*10)*cosd(xm)+200*cosd(y*10)*sind(xm))+maxx div 2,round(200*sind(x*10)*sind(y*10)*cosd(ym)+(200*cosd(y*10)*cosd(xm)-200*cosd(x*10)*sind(y*10)*sind(xm))*sind(ym))+maxy div 2,1,1,7)
        end for
    end for
    View.Update
end loop

A sphere rotating with your mouse in 12 lines.
The formula can be changed to incorporate any 3D formula, although they may not turn out that great being as it is just dots.

Author:  copthesaint [ Thu Mar 05, 2009 8:59 pm ]
Post subject:  Re: The 20line or less weekly contest!

Wow cool effect. I was very interested in you sphere. But I didn't know for sure how you did it at first. This may help others understand too what it does is basicly makes 18 dots 18 times and gives the illusion of 3D Gj Bean and don't give me credit for this. I am just posting this code for those who don't understand it. (Like me)

Turing:

View.Set ("graphics:max,max,offscreenonly")
var xm, ym, bm : int
var X : array 1 .. 4 of int
var Y : array 1 .. 4 of int
loop
    Mouse.Where (xm, ym, bm)
    cls
    for x : -18 .. 18
        for y : -18 .. 18
        /*
            for i : 1 .. 2
                X (i) := round (200 * cosd ((x+i-3) * 10) * sind ((y+i-3) * 10) * cosd (xm) + 200 * cosd ((y+i-3) * 10) * sind (xm)) + maxx div 2
                Y (i) := round (200 * sind ((x+i-3) * 10) * sind ((y+i-3) * 10) * cosd (ym) + (200 * cosd ((y+i-3) * 10) * cosd (xm) - 200 * cosd ((x+i-3) * 10) * sind ((y+i-3) * 10) * sind (xm)) * sind (ym)) + maxy div 2
            end for
            for i : 3 .. 4
                X (i) := round (200 * cosd ((x+i+1) * 10) * sind ((y+i+1) * 10) * cosd (xm) + 200 * cosd ((y+i+1) * 10) * sind (xm)) + maxx div 2
                Y (i) := round (200 * sind ((x+i+1) * 10) * sind ((y+i+1) * 10) * cosd (ym) + (200 * cosd ((y+i+1) * 10) * cosd (xm) - 200 * cosd ((x+i+1) * 10) * sind ((y+i+1) * 10) * sind (xm)) * sind (ym)) + maxy div 2
            end for
            */

            Draw.Polygon (X, Y, 4, grey)
            Draw.FillOval (round (200 * cosd (x * 10) * sind (y * 10) * cosd (xm) + 200 * cosd (y * 10) * sind (xm)) + maxx div 2, round (200 * sind (x * 10) * sind (y * 10) * cosd (ym) + (200 *
                cosd (y * 10) * cosd (xm) - 200 * cosd (x * 10) * sind (y * 10) * sind (xm)) * sind (ym)) + maxy div 2, 1, 1, 7)
            Draw.Line (round (200 * cosd (x * 10) * sind (y * 10) * cosd (xm) + 200 * cosd (y * 10) * sind (xm)) + maxx div 2, round (200 * sind (x * 10) * sind (y * 10) * cosd (ym) + (200 *
                cosd (y * 10) * cosd (xm) - 200 * cosd (x * 10) * sind (y * 10) * sind (xm)) * sind (ym)) + maxy div 2,round (200 * cosd ((x+1) * 10) * sind ((y+1) * 10) * cosd (xm) + 200 * cosd ((y+1) * 10) * sind (xm)) + maxx div 2, round (200 * sind ((x+1) * 10) * sind ((y+1) * 10) * cosd (ym) + (200 *
                cosd ((y+1) * 10) * cosd (xm) - 200 * cosd ((x+1) * 10) * sind ((y+1) * 10) * sind (xm)) * sind (ym)) + maxy div 2, black)             
        end for
    end for
    View.Update
end loop

Author:  saltpro15 [ Thu Mar 05, 2009 9:25 pm ]
Post subject:  RE:The 20line or less weekly contest!

The_Bean, that is so cool! and it's not that difficult to understand

Author:  The_Bean [ Thu Mar 05, 2009 9:44 pm ]
Post subject:  Re: The 20line or less weekly contest!

Heres what is going in those giant lines of Drawing:
The 2 for loops going -18..18 are the degrees, I multiply them by 10 to get -180..180 by 10 which is a complete 360.
Take the equation of a sphere:
code:

 xCoordinate = cosd (x) * sind (y) * radius
 yCoordinate = sind (x) * sind (y) * radius
 zCoordinate = cosd (y) * radius

Now look at the rotation formula:
code:

    p1 = p1 * cosd (theta) + p2 * sind (theta)
    p2 = p2 * cosd (theta) - p1 * sind (theta)

Where p1 and p2 are either x,y,z depending on how you want it rotated and theta controls the degrees of rotation.
To make it spin horizontally: p1=xCoordinate and p2=zCoordinate and theta=xm
To make it spin vertically: p1=yCoordinate and p2=zCoordinate and theta=ym
Now replace variables where you can, and remember you need to keep the z value change in horizontal spinning in the verticle spinning:
code:

xNewCoordinate= xCoordinate * cosd (xm) + zCoordinate * sind (xm)
zNewCoordinate= zCoordinate * cosd (xm) - xCoordinate * sind (xm)
yNewCoordinate= yCoordinate * cosd (ym) + zNewCoordinate * sind (ym)

%do some more substitution, and radius=200
xNewCoordinate = cosd (x) * sind (y) * 200 * cosd (xm) + cosd (y) * 200 * sind (xm)
zNewCoordinate = cosd (y) * 200 * cosd (xm) - cosd (x) * sind (y) * 200 * sind (xm)
yNewCoordinate = sind (x) * sind (y) * 200 * cosd (ym) + (cosd (y) * 200 * cosd (xm) - cosd (x) * sind (y) * 200 * sind (xm)* sind (ym))

Draw.Dot(round(xNewCoordinate )+maxx div 2,round(yNewCoordinate)+maxy div 2,7)

You can change the formula of a sphere to get different looking shapes.

Author:  Tony [ Thu Mar 05, 2009 10:59 pm ]
Post subject:  Re: The 20line or less weekly contest!

The_Bean @ Thu Mar 05, 2009 9:44 pm wrote:
The 2 for loops going -18..18 are the degrees, I multiply them by 10 to get -180..180 by 10 which is a complete 360.

Turing actually supports
Turing:

for i: -180 .. 180 by 10

type of syntax. for

Author:  copthesaint [ Fri Mar 06, 2009 2:52 am ]
Post subject:  RE:The 20line or less weekly contest!

tony the 180 by 10 what is it used for/ how would you use it?

Author:  zylum [ Fri Mar 06, 2009 3:30 am ]
Post subject:  RE:The 20line or less weekly contest!

copthesaint, the code Tony posted iterates from -180 to 180 by increments of 10.. So instead of writing:

code:
for x : -18 .. 18

...

sin(x * 10)


you would simply have

code:
for x : -180 .. 180 by 10

...

sin(x)



All of these 3D submissions remind me of one I made in a previous 20 line contest...

Turing:
setscreen ("offscreenonly,graphics:400;400,nobuttonbar,title:zylum3Dv3")
var x, y : array 1 .. 4 of int
var c : array 1 .. 78 of real := init (-1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, - 1, -1, 1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0)
for a : 0 .. maxint
    for i : 0 .. 60 by 12
        for j : 0 .. 9 by 3
            c (74 + j div 3) := ((cosd (a / 9) * sind (a / 8) * cosd (a / 7) + sind (a / 9) * sind (a / 7)) * c (i + j + 1) - c (i + j + 2) * sind (a / 9) * cosd (a / 7) + c (i + j + 3) * cosd (a / 9) * cosd (a / 8) + c (i + j + 2) * cosd (a / 9) * sind (a / 8) * sind (a / 7))
            x (j div 3 + 1) := round ((cosd (a / 8) * cosd (a / 7) * c (i + j + 1) + c (i + j + 2) * sind (a / 7) * cosd (a / 8) - c (i + j + 3) * sind (a / 8)) * 100 / (c (74 + j div 3) + 3) + maxx div 2)
            y (j div 3 + 1) := round (((sind (a / 9) * sind (a / 8) * cosd (a / 7) - cosd (a / 9) * sind (a / 7)) * c (i + j + 1) + c (i + j + 2) * cosd (a / 9) * cosd (a / 7) + c (i + j + 3) * sind (a / 9) * cosd (a / 8) + c (i + j + 2) * sind (a / 9) * sind (a / 8) * sind (a / 7)) * 100 / (c (74 + j div 3) + 3) + maxy div 2)
        end for
        c (78) := ((x (4) - x (3)) * (y (2) - y (3)) - (y (4) - y (3)) * (x (2) - x (3))) / (2 * 10186) + 1e-3
        drawfillpolygon (x, y, (c (78) + abs(c (78))) div c (78) * 2, RGB.AddColor (c (78) + 0.25, c (78) + 0.25, c (78) + 0.25))
    end for
    View.Update
    drawfillbox(0, 0, maxx, maxy, 43)
end for


Perspective, lighting and back face culling XD

Author:  Tallguy [ Fri Mar 06, 2009 9:23 am ]
Post subject:  RE:The 20line or less weekly contest!

i gotta say, @The_Bean - so sweet!! i love the sphere, how long did it take you to make?

Author:  MihaiG [ Fri Mar 06, 2009 3:02 pm ]
Post subject:  Re: The 20line or less weekly contest!

i hearby disband zylum from posting any more submissions Razz so other people can win Smile <3

very nice zylum, want to add some commenting to your submission?

Author:  copthesaint [ Mon Mar 23, 2009 10:56 pm ]
Post subject:  Re: The 20line or less weekly contest!

Here is a 18 line program that is easy and cool. I took 5 min to make it.

Turing:
var Num : int := 0
var BinaryTable, BinaryNumbers : array 1 .. 8 of int := init (1, 2, 4, 8, 16, 32, 64, 128)
loop
    for i : 1 .. 8
        BinaryNumbers (i) := 0
    end for
    put skip, "Enter a number less then 256 to get it's 8-bit binary form." ..
    get Num
    for decreasing i : 8 .. 1
        if Num - BinaryTable (i) >= 0 then
            BinaryNumbers (i) += 1
            Num := Num - BinaryTable (i)
        end if
    end for
    for i : 1 .. 8
        put BinaryNumbers (i), " " ..
    end for
end loop

Author:  zero-impact [ Sat Mar 28, 2009 1:29 pm ]
Post subject:  Re: The 20line or less weekly contest!

Here is a particularly nice looking zoom of the mandelbrot set. In 18 lines! Very Happy

Turing:

setscreen ("graphics:200;200;nobuttonbar;")
var a1, b1, b2 : real
var lp : nat
for x : -100 .. 100
    for y : -100 .. 100
        a1 := (0.143 + x * 0.00001)
        b1 := 0.652 + y * 0.00001
        lp := 0
        loop
            lp += 1
            b2 := 2 * a1 * b1 + 0.652 + y * 0.00001
            a1 := a1 * a1 - b1 * b1 + 0.143 + x * 0.00001
            b1 := b2
            exit when lp > 254 or ((a1 * a1) + (b1 * b1)) > 4
        end loop
        drawdot (x + 100, y + 100, lp)
    end for
end for

Author:  copthesaint [ Wed Apr 01, 2009 12:15 pm ]
Post subject:  Re: The 20line or less weekly contest!

Here is a 19 line program that will take a string and split up the values inbetween each space.
This is usefull for online programs and loading txt files.

Turing:
var First, Last, NextValue : int
var Words : array 1 .. 10000 of string
procedure GetStrings (S : string)
    First := 1
    NextValue := 0
    for i : 1 .. length (S)
        if S (i) = " " then
            Last := i - 1
            NextValue += 1
            Words (NextValue) := ""
            for s : First .. Last
                Words (NextValue) += S (s)
            end for
            First := i + 1
        end if
    end for
end GetStrings
GetStrings ("Enter any string and the program will split it and give the number of words")

Author:  saltpro15 [ Wed Apr 01, 2009 3:44 pm ]
Post subject:  RE:The 20line or less weekly contest!

here is hello world

Turing:

put "Hello World!"


in 1 line! how amazing!

Author:  SNIPERDUDE [ Wed Apr 01, 2009 3:57 pm ]
Post subject:  RE:The 20line or less weekly contest!

I vote saltpro15 the weekly winner. Razz

Author:  saltpro15 [ Wed Apr 01, 2009 4:43 pm ]
Post subject:  RE:The 20line or less weekly contest!

yay, thanks SNIPERDUDE :p also, excellent video in your signature, I don't even like hip-hop and I thought it was great

Author:  copthesaint [ Wed Apr 01, 2009 5:08 pm ]
Post subject:  RE:The 20line or less weekly contest!

Umm What are you two talking about... This is Kinda off topic don't you think...

Author:  saltpro15 [ Wed Apr 01, 2009 5:28 pm ]
Post subject:  RE:The 20line or less weekly contest!

Yes it is, my bad. I should get cracking on a real submission

Author:  Zren [ Wed Apr 01, 2009 8:08 pm ]
Post subject:  Re: The 20line or less weekly contest!

copthesaint @ Wed Apr 01, 2009 12:15 pm wrote:
Here is a 19 line program that will take a string and split up the values inbetween each space.
This is usefull for online programs and loading txt files.

Turing:
var First, Last, NextValue : int
var Words : array 1 .. 10000 of string
procedure GetStrings (S : string)
    First := 1
    NextValue := 0
    for i : 1 .. length (S)
        if S (i) = " " then
            Last := i - 1
            NextValue += 1
            Words (NextValue) := ""
            for s : First .. Last
                Words (NextValue) += S (s)
            end for
            First := i + 1
        end if
    end for
end GetStrings
GetStrings ("Enter any string and the program will split it and give the number of words")


Your program doesn't catch the last word into the array without having a trailing space. Just pointing that out dude. Good try though.

Author:  SNIPERDUDE [ Wed Apr 01, 2009 10:56 pm ]
Post subject:  RE:The 20line or less weekly contest!

do note the difficulty it would be to add error checking with such limited room. Although just to make it work in this case have the function just add one to the output number.

Author:  BBPProductions [ Thu Jun 04, 2009 9:42 am ]
Post subject:  RE:The 20line or less weekly contest!

I relly liked the sphere!

Author:  stas054 [ Sat Jun 13, 2009 7:58 pm ]
Post subject:  RE:The 20line or less weekly contest!

var pie:string:="I <3 pie"
put pie

Author:  stas054 [ Sat Jun 13, 2009 8:03 pm ]
Post subject:  Re: The 20line or less weekly contest!

The_Bean @ Thu Mar 05, 2009 6:47 pm wrote:
Did someone say 3D?
Turing:

View.Set ("graphics:500,500,offscreenonly")
var xm, ym, bm : int
loop
    Mouse.Where (xm, ym, bm)
    cls
    for x : -18 .. 18
        for y : -18 .. 18
            Draw.FillOval(round(200*cosd(x*10)*sind(y*10)*cosd(xm)+200*cosd(y*10)*sind(xm))+maxx div 2,round(200*sind(x*10)*sind(y*10)*cosd(ym)+(200*cosd(y*10)*cosd(xm)-200*cosd(x*10)*sind(y*10)*sind(xm))*sind(ym))+maxy div 2,1,1,7)
        end for
    end for
    View.Update
end loop

A sphere rotating with your mouse in 12 lines.
The formula can be changed to incorporate any 3D formula, although they may not turn out that great being as it is just dots.


suggestion: making it white on the black background would look better Razz

Author:  Carey [ Tue Aug 25, 2009 6:17 pm ]
Post subject:  Re: The 20line or less weekly contest!

Turing:

View.Set ("graphics:500,500,offscreenonly")
var xm, ym, bm : int
loop
    Mouse.Where (xm, ym, bm)
    Draw.FillBox (0, 0, 500, 500, 7)
    for x : -18 .. 18
        for y : -18 .. 18
            Draw.FillOval (round (200 * cosd (x * 10) * sind (y * 10) * cosd (xm) + 200 * cosd (y * 10) * sind (xm)) + maxx div 2, round (200 * sind (x * 10) * sind (y * 10) * cosd (ym) + (200 * cosd (y * 10) * cosd (xm) - 200 * cosd (x * 10) * sind (y * 10) * sind (xm)) * sind (ym)) + maxy div 2, 1, 1, 0)
        end for
    end for
    View.Update
end loop


Like that?

Author:  Magix [ Sun Oct 25, 2009 3:57 pm ]
Post subject:  [SUBMISSION] Image 2 Source Convertor

A new submission:

Note: This is note my code. I found it somewhere on this forum, and all I did was compress it from 42 lines of code to 18 lines of code, while still following proper coding etiquette. I take no credit for this, I just think that it is very worthy of winning an award on this contest.

Anyways, it is an image-to-source convertor that can take any image and convert it to a turing file using the Draw.Dot command. Its rather awesome, considering its only 18 lines of code. Here it is...

code:
var image, stream : int
var filename : string
    cls
    put "Enter the image file location: " ..
    get filename : *
View.Set ("graphics:" + intstr (Pic.Width (image)) + "," + intstr (Pic.Height (image)) + ",nobuttonbar")
Pic.Draw (image, 0, 0, picCopy)
open : stream, filename + ".t", put
put : stream, "View.Set (\"graphics:" + intstr (Pic.Width (image)) + "," + intstr (Pic.Height (image)) + ",nobuttonbar\")"
for x : 1 .. maxx
    View.Set ("title:Converting..." + intstr (round ((x / maxx * 100))) + "%")
    for y : 1 .. maxy
        if not whatdotcolor (x, y) = white then
            put : stream, "Draw.Dot (" + intstr (x) + ", " + intstr (y) + ", " + intstr (whatdotcolor (x, y)) + ")"
        end if
    end for
end for
close : stream


Just in case you are wondering how I compressed it so much, I removed the code to check if the image exists, and I removed the code to check if the filename of the output file is taken or not. If you are interested, here is the original code...

code:

%State variables
var image : int
var filename : string
var stream : int

%Start of loop
loop
    cls
    put "Enter the image file location: " ..
    get filename : *
    if File.Exists (filename) then
        image := Pic.FileNew (filename)
        exit
    else
        put "The file does not exist. Press any key to try again..."
        Input.Pause
    end if
end loop
%End of loop

%"If the image exists...
if File.Exists (filename + ".t") then
    put "When converting your image to a Turing source file, the file " + filename + ".t will be overwritten."
    put "Press any key to continue..."
    Input.Pause
end if
%End of "If" statement

View.Set ("graphics:" + intstr (Pic.Width (image)) + "," + intstr (Pic.Height (image)) + ",nobuttonbar")
Pic.Draw (image, 0, 0, picCopy)
open : stream, filename + ".t", put
put : stream, "View.Set (\"graphics:" + intstr (Pic.Width (image)) + "," + intstr (Pic.Height (image)) + ",nobuttonbar\")"
for x : 1 .. maxx
    View.Set ("title:Converting..." + intstr (round ((x / maxx * 100))) + "%")
    for y : 1 .. maxy
        if not whatdotcolor (x, y) = white then
            put : stream, "Draw.Dot (" + intstr (x) + ", " + intstr (y) + ", " + intstr (whatdotcolor (x, y)) + ")"
        end if
    end for
end for
close : stream


To make this work, put enter the image location when prompted for it. If it is in the same directory as the Turing Code, just type in the filename with the extension. Example: filename.jpg.
If it is the a different directory, use /, ./ and ../ commands to show this. This image only recognizes BMP and JPG images.

I attached a sample of what this program can do.

Author:  A.J [ Sun Oct 25, 2009 6:16 pm ]
Post subject:  RE:The 20line or less weekly contest!

You are missing something...

add the following line after getting the filename:
Turing:

image := Pic.FileNew (filename)

Author:  Magix [ Sun Oct 25, 2009 6:24 pm ]
Post subject:  RE:The 20line or less weekly contest!

Oh! Sorry I didn't even notice that! Thanks! So the full code would go...

code:
var image, stream : int
var filename : string
    cls
    put "Enter the image file location: " ..
    get filename : *
image := Pic.FileNew (filename)
View.Set ("graphics:" + intstr (Pic.Width (image)) + "," + intstr (Pic.Height (image)) + ",nobuttonbar")
Pic.Draw (image, 0, 0, picCopy)
open : stream, filename + ".t", put
put : stream, "View.Set (\"graphics:" + intstr (Pic.Width (image)) + "," + intstr (Pic.Height (image)) + ",nobuttonbar\")"
for x : 1 .. maxx
    View.Set ("title:Converting..." + intstr (round ((x / maxx * 100))) + "%")
    for y : 1 .. maxy
        if not whatdotcolor (x, y) = white then
            put : stream, "Draw.Dot (" + intstr (x) + ", " + intstr (y) + ", " + intstr (whatdotcolor (x, y)) + ")"
        end if
    end for
end for
close : stream

Author:  SNIPERDUDE [ Mon Oct 26, 2009 3:58 pm ]
Post subject:  Re: RE:The 20line or less weekly contest!

Magix @ October 25th 2009 wrote:
Oh! Sorry I didn't even notice that! Thanks! So the full code would go...

code:
var image, stream : int
var filename : string
    cls
    put "Enter the image file location: " ..
    get filename : *
image := Pic.FileNew (filename)
View.Set ("graphics:" + intstr (Pic.Width (image)) + "," + intstr (Pic.Height (image)) + ",nobuttonbar")
Pic.Draw (image, 0, 0, picCopy)
open : stream, filename + ".t", put
put : stream, "View.Set (\"graphics:" + intstr (Pic.Width (image)) + "," + intstr (Pic.Height (image)) + ",nobuttonbar\")"
for x : 1 .. maxx
    View.Set ("title:Converting..." + intstr (round ((x / maxx * 100))) + "%")
    for y : 1 .. maxy
        if not whatdotcolor (x, y) = white then
            put : stream, "Draw.Dot (" + intstr (x) + ", " + intstr (y) + ", " + intstr (whatdotcolor (x, y)) + ")"
        end if
    end for
end for
close : stream


I was messing with the code (it would be over 20 lines now), and made it much more efficient by putting some of those repetitive values in for loops. This not only significantly reduced the length of the output file (9.1X on one test, 11.9X on another), but also really sped up the process. I can send you the code if you wish, but this is probably not the right thread.

I was bored really.

Author:  petree08 [ Thu Nov 12, 2009 9:36 am ]
Post subject:  Re: The 20line or less weekly contest!

Here is my Rorschach ink blot generator


code:

var X, Y : int
X := 1
Y := maxy div 2
loop
    X := X + Rand.Int (-2, 2)
    Y := Y + Rand.Int (-2, 2)
    if X < -maxx or X > maxx or Y < 1 or Y > maxy then
        X := 1
        Y := maxy div 2
    end if
    drawfilloval ((maxx div 2) + X, Y, 2, 2, 7)
    drawfilloval ((maxx div 2) - X, Y, 2, 2, 7)
    View.Update
end loop


Author:  petree08 [ Thu Nov 12, 2009 9:45 am ]
Post subject:  RE:The 20line or less weekly contest!

so i don't know if it is an ink blot generator any more but i messed with it more and got this

[code]
var X, Y : int
X := 1
Y := maxy div 2
for C : 1 .. Rand.Int (800, 1700)
X := X + Rand.Int (-1, 1)
Y := Y + Rand.Int (-1, 1)
if X < -maxx or X > maxx or Y < 1 or Y > maxy then
X := 1
Y := maxy div 2
end if
drawdot ((maxx div 2) + X, Y, 7)
drawdot ((maxx div 2) - X, Y, 7)
View.Update
end for

[\code]


i like this one better

Author:  mirhagk [ Tue Nov 24, 2009 12:25 pm ]
Post subject:  Re: The 20line or less weekly contest!

um so yeah they should have posted a line length limit or else i could just do this

its mona lisa and random special drawing effects in 11 lines of code. (without special effects its 7 lines of code).

no i did not use a picture file at all, it draws it point by point.

Wanna know how i did it, well first i modified the earlier picture to turing code into the following

code:

var image, stream : int
var filename : string
cls
put "Enter the image file location: " ..
get filename : *
image := Pic.FileNew (filename)
View.Set ("graphics:" + intstr (Pic.Width (image)) + "," + intstr (Pic.Height (image)) + ",nobuttonbar")
Pic.Draw (image, 0, 0, picCopy)
open : stream, filename + ".t", put
put : stream, "View.Set (\"graphics:" + intstr (Pic.Width (image)) + "," + intstr (Pic.Height (image)) + ",nobuttonbar\")"
put : stream, "var pic:array 1.." + intstr (Pic.Width (image)) + ",1.." + intstr (Pic.Height (image)) + " of int:=init(" ..
for x : 1 .. Pic.Width (image)
    View.Set ("title:Converting..." + intstr (round ((x / maxx * 100))) + "%")
    for y : 1 .. Pic.Height (image)
        put : stream, intstr (whatdotcolor (x, y)) + "," ..
    end for
end for
put : stream, ")\nfor x:1.." + intstr (Pic.Width (image)) + "\n\tfor y:1.." + intstr (Pic.Height (image)) + "\n\t\t" ..
put : stream, "Draw.Dot(x,y,pic(x,y))\n\tend for\nend for"
close : stream


instead of thousands of Draw.Dot commands (and close to a million kb for some pictures) it creates an array of all the points to store a colour variable, then it adds the code to use a for loop and draw the picture using the array.

then i opened the created 7 line program and proceeded to add the special effects.
try it out, and you can create your own programs like this using the above code (which still qualifies for the 20 lines or less btw)

Author:  Zasalamel [ Mon Nov 30, 2009 7:00 pm ]
Post subject:  RE:The 20line or less weekly contest!

Yeah mirhagk! i didn't think you would post it...

Author:  mirhagk [ Tue Dec 08, 2009 12:26 pm ]
Post subject:  RE:The 20line or less weekly contest!

didn't think I'd post what?? the mona lisa thing, or the code used to make it??

Author:  Superskull85 [ Wed Dec 09, 2009 12:02 am ]
Post subject:  Re: The 20line or less weekly contest!

I was wondering how small I could make my base conversion program, and still make it usable (not entirely "the best").

Any base 2-36 to decimal (10):

Turing:
var Num : string
function IsValid (Num : string) : boolean
    if length (Num) not= 0 then
        result ((ord (Num (1)) >= 48 and ord (Num (1)) <= 57) or (ord (Str.Upper (Num (1))) >= 65 and ord (Str.Upper (Num (1))) <= 90)) and IsValid (Num (2 .. *))
    end if
    result true
end IsValid
function ConvertBaseDec (Num : string, Base : int) : int
    if length (Num) = 0 or not IsValid (Num) then
        result 0
    end if
    if ord (Num (1)) >= 48 and ord (Num (1)) <= 57 then
        result strint (Num (1)) * (Base ** (length (Num) - 1)) + ConvertBaseDec (Num (2 .. *), Base)
    else
        result (ord (Num (1)) - 55) * (Base ** (length (Num) - 1)) + ConvertBaseDec (Num (2 .. *), Base)
    end if
end ConvertBaseDec
put "Input a base 2-36 number, and the base (number:base): " ..
get Num
put ConvertBaseDec (Num (1 .. index (Num, ":") - 1), strint (Num (index (Num, ":") + 1 .. *)))

Decimal (10) to any base 2-36:

Turing:
var Num : string
function SetValue (Num : int) : string
    if Num >= 10 then
        result chr (Num - 10 + 65)
    else
        result intstr (Num)
    end if
end SetValue
function ConvertDecBase (Num, Base : int) : string
    if Num = 0 then
        result ""
    end if
    result ConvertDecBase (Num div Base, Base) + SetValue (Num mod Base)
end ConvertDecBase
put "Input a decimal number, and a base (number:base): " ..
get Num
if strintok (Num (1 .. index (Num, ":") - 1)) then
    put ConvertDecBase (strint (Num (1 .. index (Num, ":") - 1)), strint (Num (index (Num, ":") + 1 .. *)))
end if

I am pretty sure the algorithms will convert any number given. If not let me know. Smile

I know this is not the best algorithm that could be designed, but it gave me a challenge and made me look at the problem in a whole different way.

Author:  Tony [ Wed Dec 09, 2009 12:12 am ]
Post subject:  Re: The 20line or less weekly contest!

Superskull85 @ Wed Dec 09, 2009 12:02 am wrote:
I know this is not the best algorithm that could be designed

Wait, is the algorithm "use strint", or are you talking about validation/parsing-input?

Author:  Superskull85 [ Wed Dec 09, 2009 6:10 pm ]
Post subject:  Re: The 20line or less weekly contest!

Using strint would accomplish the same thing but would not allow you to express numbers in other bases as you would be limited to the bases 2 through 36.

Using my method you could convert a base one number, such as "11111" into decimal (would be five) if you wanted to (you cannot convert decimal to base one using my method).

Also by adding/changing a couple of lines you could represent all the bases between 2 and 62 using all numerical characters (0-9), and all upper case (A-Z) and lower case (a-z) letters.

Any base 1-36 to decimal (10):
Turing:
var Num : string
function IsValid (Num : string) : boolean
    if length (Num) not= 0 then
        result ((ord (Num (1)) >= 48 and ord (Num (1)) <= 57) or
                (ord (Str.Upper (Num (1))) >= 65 and ord (Str.Upper (Num (1))) <= 90) or
                (ord (Str.Upper (Num (1))) >= 97 and ord (Str.Upper (Num (1))) <= 122))
                and IsValid (Num (2 .. *))
    end if
    result true
end IsValid
function ConvertBaseDec (Num : string, Base : int) : int
    if length (Num) = 0 or not IsValid (Num) then
        result 0
    end if
    if ord (Num (1)) >= 48 and ord (Num (1)) <= 57 then
        result strint (Num (1)) * (Base ** (length (Num) - 1)) + ConvertBaseDec (Num (2 .. *), Base)
    elsif ord (Num (1)) >= 65 and ord (Num (1)) <= 90 then
        result (ord (Num (1)) - 55) * (Base ** (length (Num) - 1)) + ConvertBaseDec (Num (2 .. *), Base)
    else
        result (ord (Num (1)) - 61) * (Base ** (length (Num) - 1)) + ConvertBaseDec (Num (2 .. *), Base)       
    end if
end ConvertBaseDec
put "Input a base 1-62 number, and the base (number:base): " ..
get Num
put ConvertBaseDec (Num (1 .. index (Num, ":") - 1), strint (Num (index (Num, ":") + 1 .. *)))


Any decimal (10) to base 2-62:
Turing:
var Num : string
function SetValue (Num : int) : string
    if Num >= 36 then
        result chr (Num - 36 + 97)
    elsif Num >= 10 then
        result chr (Num - 10 + 65)
    else
        result intstr (Num)
    end if
end SetValue
function ConvertDecBase (Num, Base : int) : string
    if Num = 0 then
        result ""
    end if
    result ConvertDecBase (Num div Base, Base) + SetValue (Num mod Base)
end ConvertDecBase
put "Input a decimal number, and a base (2-62) (number:base): " ..
get Num
if strintok (Num (1 .. index (Num, ":") - 1)) then
    put ConvertDecBase (strint (Num (1 .. index (Num, ":") - 1)), strint (Num (index (Num, ":") + 1 .. *)))
end if

You cannot do this with strint.

So to be specific I should of said, "I was wondering how small I could make my base conversion program, without using strint and still make it usable (not entirely "the best")." instead in my previous post.

Author:  Tony [ Wed Dec 09, 2009 6:37 pm ]
Post subject:  RE:The 20line or less weekly contest!

Oh, I see what's going on now. At first I just saw the use of strint, but now your description also explains how this works beyond the limitations. Nice.

Author:  Magix [ Thu Dec 10, 2009 5:55 pm ]
Post subject:  Another Submission

Here I am with another submission...a prime number finder... with Turing! You enter a low number and a high number, and the program can list out for you all the prime numbers between them, and in the end, it outputs the total number of prime numbers. Only problem is that, because of my noob coding and Turing's un-flexibility, it runs very slowly if the gap between the numbers you enter is too big. If you enter 1 and 10,000, it runs just fine, but enter 1 and 100,000, and the thing gets slower. It still works though.

code:

var iNum1, iNum2, iDiv, iPrm : int
iPrm := 0                                   %Initial value
put "Starting number: " ..                  %Caption
get iNum1                                   %Get lower number
put "Ending number: " ..                    %Caption
get iNum2                                   %Get higher number
put "List of prime numbers between ", iNum1, " and ", iNum2, "..."
for count : iNum1 .. iNum2                  %Starts for loop to execute
    iDiv := 0                               %Set number of factors to 0
    for fDivider : 1 .. iNum2               %Starts divident finder
        if count mod fDivider = 0 then      %If remainder=0, then
            iDiv := iDiv + 1                %Add 1 to total number of factors
        end if                              %End if
    end for                                 %End for loop
    if iDiv = 2 then                        %If it has no factors...
        iPrm := iPrm + 1                    %Add 1 to number of prime numbers
        put count                           %Output number
    end if                                  %End if
end for
put "There are ", iPrm, " prime numbers between ", iNum1, " and ", iNum2, "."


This version is super-compressed to fit into 20 lines. If you want to see the full version, click here.

Author:  mirhagk [ Sun Dec 13, 2009 9:58 am ]
Post subject:  RE:The 20line or less weekly contest!

Hey just a little tip
this section:

Turing:

for fDivider : 1 .. iNum2
    if count mod fDivider = 0 then
        iDiv := iDiv + 1
    end if
end for

you don't need to check if it's divisible by one right? so start it at 2 (will increase speed a little)
also you already know it's divisble by itself and not divisible by numbers greater than itself. So you only need to check for numbers smaller than itself (count-1)
heres what that section should look like (also remember to change the if statement checking how many factors it has, it should have 0 with the new improvements to the code)

Turing:

for fDivider : 2 .. count-1             
        if count mod fDivider = 0 then     
            iDiv := iDiv + 1               
        end if                             
    end for

Author:  Tony [ Sun Dec 13, 2009 1:56 pm ]
Post subject:  Re: RE:The 20line or less weekly contest!

mirhagk @ Sun Dec 13, 2009 9:58 am wrote:
So you only need to check for numbers smaller than itself (count-1)

One might notice that there is a lot more numbers one does not need to check to see if a number is prime or not.

Do we need to check count-2? -3? How many can we take off, and still be certain in the true/false answer for number being prime?

Author:  DemonWasp [ Sun Dec 13, 2009 11:32 pm ]
Post subject:  RE:The 20line or less weekly contest!

...and do we really need to know how many factors the number has? Isn't it enough to know that it has factors other than 1 and itself?

Author:  mirhagk [ Sun Dec 13, 2009 11:37 pm ]
Post subject:  RE:The 20line or less weekly contest!

wouldnt it be more effecient to keep an array of all the numbers set to true then set them to false if you find a number it's divisible by.

then when your checking the number you can check against only if the number is true (so it only checks against prime numbers).

Author:  ecookman [ Sun Dec 13, 2009 11:39 pm ]
Post subject:  RE:The 20line or less weekly contest!

some really cool stuff here, I am just for fun going to see if I can make anything like these. Wait, isn't it possible to code everything in one line?

Author:  mirhagk [ Sun Dec 13, 2009 11:43 pm ]
Post subject:  RE:The 20line or less weekly contest!

with a ; but otherwise no. (well maybe some programs but not most useful programs)

Author:  Tony [ Sun Dec 13, 2009 11:57 pm ]
Post subject:  Re: RE:The 20line or less weekly contest!

mirhagk @ Sun Dec 13, 2009 11:37 pm wrote:
wouldnt it be more effecient to keep an array of all the numbers...

If you want to check a range of numbers, yes, you can use a sieve (and it would be ridiculously better); but it's not really applicable if we are checking just one number (well, it could be used in a way that we find all the primes up the current number we are checking, but that's extra work).

Author:  mirhagk [ Mon Dec 14, 2009 2:09 am ]
Post subject:  RE:The 20line or less weekly contest!

well the program i have in mind would cancel out checking numbers if they have been declared by the program to be non-prime

Author:  deltamanx [ Fri Jan 08, 2010 9:24 pm ]
Post subject:  Re: The 20line or less weekly contest!

This might not look like much; but there's a reason I put it here.

code:

loop
     put "NOOB" ..
end loop


This would normally do nothing special right...
But guess what happened when I ran this with arguments to save everything it outputted to a file.
And then left it running while I was off on a 10 day vacation?
Result?
8.67 GB file composed of only NOOBNOOBNOOBNOOB...
I would have attached, but you put a limit. Razz

So ya. Now take a look at this in perspective...

Author:  chrisbrown [ Fri Jan 08, 2010 10:01 pm ]
Post subject:  RE:The 20line or less weekly contest!

What a fantastic waste of computing power. IMO, each NOOB you have there increases your rank on the noob scale.

Also: you could have had a file 4-6 times bigger in the same time had you used a different language... just for perspective.

Author:  deltamanx [ Fri Jan 08, 2010 10:22 pm ]
Post subject:  Re: The 20line or less weekly contest!

Above was a joke submission, here's my real one.

code:
%Made by Deltamanx
setscreen ("graphics:600,600")
var a, b, v, x, y: int := 1
for z : 0 .. 800 by 25
    drawline (b, a, x + z, y, black)
    drawline (a, b, y, x + z, black)
    drawline (b, a, y, x + z, yellow)
    drawline (a, b,  x + z,y, yellow)
    drawline (1, 1, 600, 600 - z, blue)
    drawline (1, 1 + z, 600, 600, blue)
    drawline (300 + z, 1, 400, 600, red)
    drawline (300, 1, 200 - z, 600, red)
    drawline (1, 600 - y, 600, 600 - y, green)
    drawline (1, 1 + y, 600, 1 + y, green)
    a := x
    b := y
    v := v + 1
    y := y + v + 1
    delay (80)
end for


20 line on the mark.

I call it: Linear art.

Author:  petree08 [ Thu Apr 08, 2010 11:47 am ]
Post subject:  RE:The 20line or less weekly contest!

It's cool man, but art? maybe not.

Author:  prateke [ Wed May 12, 2010 5:50 pm ]
Post subject:  Re: The 20line or less weekly contest!

Fastest factor finder in this forum!! Cool

code:

var x : int;loop;put "Enter number to get factors of: " ..;get x;cls;for i : 1 .. x;if (x mod i) = 0 then;put "Factors of ", x, " are :", i; end if;end for;end loop


its also very extremely and awesomely fast
I can has teh award for this week? Mr. Green Wink

Author:  Tony [ Wed May 12, 2010 6:33 pm ]
Post subject:  Re: The 20line or less weekly contest!

prateke @ Wed May 12, 2010 5:50 pm wrote:
Fastest factor finder in this forum!!

You can make it run in O(logN) instead of O(N) with one tiny change.

Author:  Geniis [ Thu May 13, 2010 1:04 am ]
Post subject:  Re: The 20line or less weekly contest!

not sure about O(logN), but i got it O(sqrtN)(at least i think that's what it is) :

Turing:

const number : nat := 7637325

proc PutFactors (num : nat)
    var cur : nat := 1
    var top : nat := num
    loop
        exit when cur >= top
        if num mod cur = 0 then
            put cur, " " ..
            if cur ** 2 not= num then
                put num div cur, " " ..
            end if
        end if
        top := num div cur
        cur += 1
    end loop
end PutFactors

PutFactors (number)

Author:  Brightguy [ Thu May 13, 2010 8:57 am ]
Post subject:  Re: The 20line or less weekly contest!

Geniis @ Thu May 13, 2010 1:04 am wrote:
not sure about O(logN), but i got it O(sqrtN)(at least i think that's what it is)

Yes, O(sqrt(N)) arithmetic (not bit) operations, where N is the number to factor. The log(N) was probably a misprint: there is no known polynomial time factoring algorithm.

Author:  Tony [ Thu May 13, 2010 9:46 am ]
Post subject:  Re: The 20line or less weekly contest!

Brightguy @ Thu May 13, 2010 8:57 am wrote:
The log(N) was probably a misprint

Right. I was thinking of N^0.5 (that being sqrt(N)).

Author:  BITISCT [ Thu Aug 12, 2010 12:29 am ]
Post subject:  Re: The 20line or less weekly contest!

Very, very minimal langton's ant. Included is the original without lines cut out

Author:  goroyoshi [ Sat Apr 02, 2011 7:45 pm ]
Post subject:  Re: The 20line or less weekly contest!

are we still doing this?
if so then

Turing:

import GUI %required for buttons
setscreen ("nobuttonbar,graphics:200;200,title:Click masher") %making the screen smaller, taking out the annoying buttons at the top, and putting a title
var count, timecounted : int := 0  %the counters for clicks and time
process timecounter %counts the time and stops when at one minute then puts the amount of clicks done
    loop
        timecounted := Time.Elapsed
        exit when timecounted = 60000
    end loop
    put "In one minute you clicked ", count, " times"
end timecounter
proc lol %counter of clicks, starts on the first click
    if count = 0 then
        fork timecounter
    end if
    count += 1
end lol
var button : int := GUI.CreateButton (0, 0, 1, "Mash me", lol) %the button
loop
    exit when GUI.ProcessEvent %required to make button work
end loop

Author:  Insectoid [ Sat Apr 02, 2011 8:28 pm ]
Post subject:  RE:The 20line or less weekly contest!

If you have a look at some of the other submissions in this thread, you'll see that a button masher is not really contest material.

Author:  mirhagk [ Thu Nov 03, 2011 2:26 am ]
Post subject:  RE:The 20line or less weekly contest!

Not sure if this is still going, but I made one of the scariest programs ever. It has no variables, instead it directly references memory addresses, and it has a nasty optimization because of this. See if you can a) determine what the program does b) determine how it does it. Have fun Wink

17 lines of code, oh and make sure you input a number at the beginning
Turing:

put nat4@(65536)
get nat4@(65536)
nat2@(65540) := floor (sqrt (nat4@(65536)))
nat4@(65544) := 1
loop
    if nat4@(65536) mod nat2@(65544) = 0 then
        nat2@(65546 + (nat2@(65546) + 1) * 2) := nat2@(65544)
        nat4@(65544) += 1 shl 16 + 1
    else
        nat2@(65544) += 1
    end if
    exit when nat2@(65544) > nat2@(65540)
end loop
nat2@(65544) := 0
loop
    nat2@(65544) += 1
    put nat2@(65536) div nat2@(65546 + nat2@(65544) * 2), ", ", nat2@(65546 + nat2@(65544) * 2), ", " ..
    exit when nat2@(65544) = nat2@(65546)
end loop


EDIT: found another spot for that little optimization, and added a cool feature to the top

Author:  Beastinonyou [ Thu Nov 03, 2011 7:51 am ]
Post subject:  Re: RE:The 20line or less weekly contest!

mirhagk @ Thu Nov 03, 2011 2:26 am wrote:
a) determine what the program does


Well, it seems it returns all multiples of that number. As for how it does it.... Are you David Copperfield? or David Blaine?

Author:  mirhagk [ Thu Nov 03, 2011 5:55 pm ]
Post subject:  RE:The 20line or less weekly contest!

It's not super difficult lol, it's essentially just using assembly, have to keep a few things in mind, once you get used to numbers for variables it's not hard, just a bit confusing, I can comment it all if anyone'd like.

Author:  ClayWall [ 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! Laughing

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

Author:  Zren [ 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.

Author:  Dreadnought [ 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).

Author:  Insectoid [ 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.

Author:  Dreadnought [ 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.

Author:  Aange10 [ 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

Author:  Dreadnought [ 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)

Author:  Jeffmagma [ 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

Author:  Jeffmagma [ 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


: