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

Username:   Password: 
 RegisterRegister   
 Project Euler Problem 17
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
zero-impact




PostPosted: Mon Oct 20, 2008 4:59 pm   Post subject: Project Euler Problem 17

I'm not sure if this should be in turing help because it's not really a turing specific problem...
Anyways I'm working on Project Euler problem 17 http://projecteuler.net/index.php?section=problems&id=17 and I don't understand why this isn't working.
Some feedback would be much appreciated.
Sorry for my terrible comments in advance. I need to improve my comments that are not for school work.

Turing:

setscreen ("text")
var total : int := 0

var nums : array 0 .. 30, 0 .. 1 of string

nums (0, 0) := ""
nums (1, 0) := "one"
nums (2, 0) := "two"
nums (3, 0) := "three"
nums (4, 0) := "four"
nums (5, 0) := "five"
nums (6, 0) := "six"
nums (7, 0) := "seven"
nums (8, 0) := "eight"
nums (9, 0) := "nine"
nums (10, 0) := "ten"
nums (11, 0) := "eleven"
nums (12, 0) := "twelve"
nums (13, 0) := "thirteen"
nums (14, 0) := "fourteen"
nums (15, 0) := "fifteen"
nums (16, 0) := "sixteen"
nums (17, 0) := "seventeen"
nums (18, 0) := "eighteen"
nums (19, 0) := "nineteen"
nums (20, 0) := "twenty"
nums (21, 0) := "thirty"
nums (22, 0) := "forty"
nums (23, 0) := "fifty"
nums (24, 0) := "sixty"
nums (25, 0) := "seventy"
nums (26, 0) := "eighty"
nums (27, 0) := "ninety"
nums (28, 0) := "hundred"
nums (29, 0) := "thousand"
nums (30, 0) := "and"

for i : 0 .. 30
    nums (i, 1) := intstr (length (nums (i, 0))) %lengths of the numbers
end for

for i : 1 .. 20
    total += strint (nums (i, 1))
end for


for i : 21 .. 999

    if length (intstr (i)) = 2 then

        total += strint (nums ((i div 10) + 18, 1)) + strint (nums (i mod 10, 1)) % tens column then ones column

    elsif length (intstr (i)) = 3 & i mod 10 ~= 0 then

        total += strint (nums (i div 100, 1)) %first number
        total += strint (nums (28, 1)) %hundred
        total += strint (nums (30, 1)) %and

        if i mod 100 div 10 = 0 then
            total += strint (nums (i mod 10, 1)) % ones
        elsif i mod 100 div 10 = 1 then
            total += strint (nums ((i mod 10) + 10, 1))    % teens
        else
            total += strint (nums ((i mod 100 div 10) + 18, 1)) + strint (nums (i mod 10, 1))     % twenty+
        end if

    elsif length (intstr (i)) = 3 & i mod 10 = 0 then

        total += strint (nums (i div 100, 1))
        total += strint (nums (28, 1)) %hundred

    end if

end for
total += strint (nums (1, 1)) + strint (nums (29, 1))
put total
Sponsor
Sponsor
Sponsor
sponsor
zylum




PostPosted: Mon Oct 20, 2008 7:31 pm   Post subject: RE:Project Euler Problem 17

maybe this will help:

Turing:
setscreen ("text")
var total : int := 0

var nums : array 0 .. 30, 0 .. 1 of string

nums (0, 0) := ""
nums (1, 0) := "one"
nums (2, 0) := "two"
nums (3, 0) := "three"
nums (4, 0) := "four"
nums (5, 0) := "five"
nums (6, 0) := "six"
nums (7, 0) := "seven"
nums (8, 0) := "eight"
nums (9, 0) := "nine"
nums (10, 0) := "ten"
nums (11, 0) := "eleven"
nums (12, 0) := "twelve"
nums (13, 0) := "thirteen"
nums (14, 0) := "fourteen"
nums (15, 0) := "fifteen"
nums (16, 0) := "sixteen"
nums (17, 0) := "seventeen"
nums (18, 0) := "eighteen"
nums (19, 0) := "nineteen"
nums (20, 0) := "twenty"
nums (21, 0) := "thirty"
nums (22, 0) := "forty"
nums (23, 0) := "fifty"
nums (24, 0) := "sixty"
nums (25, 0) := "seventy"
nums (26, 0) := "eighty"
nums (27, 0) := "ninety"
nums (28, 0) := "hundred"
nums (29, 0) := "thousand"
nums (30, 0) := "and"

for i : 0 .. 30
    nums (i, 1) := intstr (length (nums (i, 0))) %lengths of the numbers
end for

for i : 1 .. 20
    total += strint (nums (i, 1))
end for

View.Set ("text")


for i : 21 .. 999

    if length (intstr (i)) = 2 then

        total += strint (nums ((i div 10) + 18, 1)) + strint (nums (i mod 10, 1)) % tens column then ones column
        %put nums ((i div 10) + 18, 0), " ", nums (i mod 10, 0)

    elsif length (intstr (i)) = 3 & i mod 10 ~= 0 then

        total += strint (nums (i div 100, 1)) %first number
        total += strint (nums (28, 1)) %hundred
        total += strint (nums (30, 1)) %and

        put nums (i div 100, 0), " ", nums (28, 0), " ", nums (30, 0), " " ..

        if i mod 100 div 10 = 0 then
            total += strint (nums (i mod 10, 1)) % ones
            put nums (i mod 10, 0)
        elsif i mod 100 div 10 = 1 then
            total += strint (nums ((i mod 10) + 10, 1))    % teens
            put nums ((i mod 10) + 10, 0)
        else
            total += strint (nums ((i mod 100 div 10) + 18, 1)) + strint (nums (i mod 10, 1))     % twenty+
            put nums ((i mod 100 div 10) + 18, 0), " ", nums (i mod 10, 0)
        end if


    elsif length (intstr (i)) = 3 & i mod 10 = 0 then

        total += strint (nums (i div 100, 1))
        total += strint (nums (28, 1)) %hundred

    end if

end for
total += strint (nums (1, 1)) + strint (nums (29, 1))
put total
zero-impact




PostPosted: Mon Oct 20, 2008 8:35 pm   Post subject: Re: Project Euler Problem 17

Haha thanks very much zylum Smile

I should have done that first sorry.
zero-impact




PostPosted: Mon Oct 20, 2008 9:13 pm   Post subject: Re: Project Euler Problem 17

Ugh finally got it after who knows how many tries! Smile

Unfortunately my code seems rather ugly and I'm sure there must have been a better way to do this.


Turing:

setscreen ("text")
var total : int := 0

var nums : array 0 .. 30, 0 .. 1 of string

nums (0, 0) := ""
nums (1, 0) := "one"
nums (2, 0) := "two"
nums (3, 0) := "three"
nums (4, 0) := "four"
nums (5, 0) := "five"
nums (6, 0) := "six"
nums (7, 0) := "seven"
nums (8, 0) := "eight"
nums (9, 0) := "nine"
nums (10, 0) := "ten"
nums (11, 0) := "eleven"
nums (12, 0) := "twelve"
nums (13, 0) := "thirteen"
nums (14, 0) := "fourteen"
nums (15, 0) := "fifteen"
nums (16, 0) := "sixteen"
nums (17, 0) := "seventeen"
nums (18, 0) := "eighteen"
nums (19, 0) := "nineteen"
nums (20, 0) := "twenty"
nums (21, 0) := "thirty"
nums (22, 0) := "forty"
nums (23, 0) := "fifty"
nums (24, 0) := "sixty"
nums (25, 0) := "seventy"
nums (26, 0) := "eighty"
nums (27, 0) := "ninety"
nums (28, 0) := "hundred"
nums (29, 0) := "thousand"
nums (30, 0) := "and"

for i : 0 .. 30
    nums (i, 1) := intstr (length (nums (i, 0))) %lengths of the numbers
end for

for i : 1 .. 20
    total += strint (nums (i, 1))
    put nums (i, 0)
end for

View.Set ("text")


for i : 21 .. 999

    if length (intstr (i)) = 2 then

        total += strint (nums ((i div 10) + 18, 1)) + strint (nums (i mod 10, 1)) % tens column then ones column
        put nums ((i div 10) + 18, 0), " ", nums (i mod 10, 0)

    elsif length (intstr (i)) = 3 & i mod 10 ~= 0 then

        total += strint (nums (i div 100, 1)) %first number
        total += strint (nums (28, 1)) %hundred
        total += strint (nums (30, 1)) %and

        put nums (i div 100, 0), " ", nums (28, 0), " ", nums (30, 0), " " ..

        if i mod 100 div 10 = 0 then
            total += strint (nums (i mod 10, 1)) % ones
            put nums (i mod 10, 0)
        elsif i mod 100 div 10 = 1 then
            total += strint (nums ((i mod 10) + 10, 1))    % teens
            put nums ((i mod 10) + 10, 0)
        else
            total += strint (nums ((i mod 100 div 10) + 18, 1)) + strint (nums (i mod 10, 1))     % twenty+
            put nums ((i mod 100 div 10) + 18, 0), " ", nums (i mod 10, 0)
        end if


    elsif length (intstr (i)) = 3 & i mod 10 = 0 then  %if the number is divisible by ten   ie. ends in a zero

        if i mod 100 = 0 then % for the hundreds
            total += strint (nums (i div 100, 1))
            total += strint (nums (28, 1)) %hundred
            put nums (i div 100, 0), " ", nums (28, 0) %tens
        elsif intstr (i) (2 .. 3) = "10" then
            total += strint (nums (i div 100, 1))
            total += strint (nums (28, 1)) %hundred
            total += strint (nums (30, 1))             %and
            total += strint (nums (10,1))
            put nums (i div 100, 0), " ", nums (28, 0), " ", nums (30,0), " ", nums (10,0)
        else
            total += strint (nums (i div 100, 1))
            total += strint (nums (28, 1)) %hundred
            total += strint (nums (30, 1)) %and
            total += strint (nums ((i mod 100 div 10) + 18, 1))
            put nums (i div 100, 0), " ", nums (28, 0), " ", nums (30, 0), " ", nums ((i mod 100 div 10) + 18, 0)
        end if

    end if

end for
total += strint (nums (1, 1)) + strint (nums (29, 1))
put nums (1, 0), " ", nums (29, 0)
put total



euler.png
 Description:
 Filesize:  48.59 KB
 Viewed:  344 Time(s)

euler.png


Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: