Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 The 20line or less weekly contest!
Index -> Programming, Turing -> Turing Submissions
Goto page Previous  1, 2, 3, 4, 5, 6  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zasalamel




PostPosted: 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...
Sponsor
Sponsor
Sponsor
sponsor
mirhagk




PostPosted: 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??
Superskull85




PostPosted: 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.
Tony




PostPosted: 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?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Superskull85




PostPosted: 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.
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Magix




PostPosted: 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.
mirhagk




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
DemonWasp




PostPosted: 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?
mirhagk




PostPosted: 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).
ecookman




PostPosted: 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?
mirhagk




PostPosted: 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)
Tony




PostPosted: 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).
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
mirhagk




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 4 of 6  [ 84 Posts ]
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Jump to:   


Style:  
Search: