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

Username:   Password: 
 RegisterRegister   
 LCD,GCM
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2, 3, 4  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MysticAngel




PostPosted: Sun Apr 06, 2003 10:13 am   Post subject: (No subject)

l how would u frame the code ?
Sponsor
Sponsor
Sponsor
sponsor
yuethomas




PostPosted: Sun Apr 06, 2003 11:27 am   Post subject: (No subject)

Oh sorry, I misunderstood.

I don't know of any algorithms, so I'll use a dumb one here...

code:
% checking proper factors of a

var a : int := 30

var sum : int := 0

% an optimisation note: when a is odd
% the largest factor cannot exceed a/3;
% when a is even it cannot exceed a/2.

if floor(a/2) = a/2 then % checks for parity

    for b : 2 .. floor (a/2)

    % even though a/2 is an integer,
    % I doubt a/2 will return a variable
    % of type integer... so just wrap it
    % in a function that returns a integer.

        if floor (a/b) = a/b then sum += b

    end for

else % a is odd

    for b : 3 .. ceil (a/3)

    % now a isn't necessarily divisible by
    % 3, so take the larger value here
    % just to be safe

        if floor (a/b) = a/b then sum += b

    end for

end if

put "SUM OF PROPER FACTORS OF ", a, " IS ", b
Prince




PostPosted: Mon Apr 07, 2003 10:02 am   Post subject: (No subject)

code:

function properFactors (n : int) : int
    var x, y, temp : int := 0
    for decreasing i : n .. 1 by 1
        x := n mod i
        if x = 0 then
            y := n div i
            if y not= n then
                temp := temp + y
            end if
        end if
    end for
    result temp
end properFactors


now doesnt that seem a whole lot simpler... it works too Very Happy
Prince




PostPosted: Mon Apr 07, 2003 10:04 am   Post subject: (No subject)

btw, yuethomas, proper factors do include 1
yuethomas




PostPosted: Mon Apr 07, 2003 4:35 pm   Post subject: (No subject)

My mistake.
MysticAngel




PostPosted: Wed Apr 09, 2003 9:19 pm   Post subject: (No subject)

Can u plz check my code. It is supposed to give the reverse of a number.
eg. 72--27


code:

function reverse (n:int) : int
    var digits,sum,r : int
    var count : int := 0
    loop
        digits := n mod 10
        r := n div 10
        count := count + 1       
        exit when count >= 0
    end loop   
    result digits
end reverse
Asok




PostPosted: Wed Apr 09, 2003 9:27 pm   Post subject: (No subject)

I believe that was allready posted for reversing numbers, actually I guarantee it. Search the forums.
MysticAngel




PostPosted: Wed Apr 09, 2003 9:35 pm   Post subject: (No subject)

but u are using instr... which i dont want to use, there are errors there anyways..... i know how to do it without the function. but when i use the function i get a bit confused like what to ouput. my code gives out i think just the first reverse number. can u plz check it Sad


code:

function reverse (n:int) : int
    var digits,sum,r : int
    var count : int := 0
    loop
        digits := n mod 10
        r := n div 10
        count := count + 1       
        exit when count >= 0
    end loop   
    result digits
end reverse
Sponsor
Sponsor
Sponsor
sponsor
yuethomas




PostPosted: Wed Apr 09, 2003 9:46 pm   Post subject: (No subject)

code:
function reverse (n:int) : int
    var digits : int
    var count : int := 0
    var r: int := n
    loop
        digits := digits * 10 + r mod 10
        r := r div 10
        exit when r = 0       
    end loop
    result digits
end reverse


Edit: (thank you Tony)
Tony




PostPosted: Wed Apr 09, 2003 9:49 pm   Post subject: (No subject)

well its all wrong.

it will result in n mod 10 which is the remainder of n after dividing by 10. Since your exit statment will get you out of loop right away. I dont even understand what you're trying to do Confused

first of all... to get the number of digits, you keep on dividing by multiples of 10 until your result is <= 0.

after that, you go in revese order starting with the largest digit, divide it by 10**(digit - 1). Floor it into int value and you get isolated digit. Then add it to new varaible, but first you need to multiply it by a multiple of 10 to place it in the right spot.

Ah damn, yuethomas beat me to it with a working code... I'm giving some bits for the help Very Happy
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
MysticAngel




PostPosted: Wed Apr 09, 2003 9:55 pm   Post subject: (No subject)

it works the way he said
MysticAngel




PostPosted: Thu Apr 10, 2003 9:59 pm   Post subject: (No subject)

k this function woud return a value that is between 0 and 9. it takes the entered number and adds its digits together. eg. 79 = 7 + 9 = 16 and 1+6 = 7. so the digital root of the 79 is 7. my function is working for some parts. not all thouhg. canu plz check the code ????
code:

function digitalroot (n : int) : int
    var digits, count, r, sum : int := 0
    loop
        count := count + 1
        digits := n mod 10
        r := n div 10
        sum := sum + r
        exit when sum >= 0 and sum <= 9   
        end loop
    result sum
end digitalroot
Prince




PostPosted: Thu Apr 10, 2003 10:14 pm   Post subject: (No subject)

1. u dont need a count for this one... 2. if ur gonna do it the way u hav it, make it so that sum := digits + r and not sum := sum + digits... heres how i did it

code:

function digitalRoot (var n : int) : int
    var a, b, c, x : int := 0
    loop
        a := n mod 10
        n := n div 10
        c := c + a
        exit when a = 0
    end loop
    a := c div 10
    b := c mod 10
    x := a + b
    result x
end digitalRoot


im no good at explainin things so im not gonna start here (even tho its my code Confused )... if i can remember wat i was thinkin wen i made this ill repost
Prince




PostPosted: Fri Apr 11, 2003 10:08 am   Post subject: (No subject)

looking at ur code again, and actually testing it this time, ive found ur error:

code:

digits := n mod 10
r := n div 10
sum := sum + r  % error on this line
exit when sum >= 0 and sum <= 9


it happens wen u split the number apart... since r is between 0 and 9 already, adding it to sum (which is 0) does nothing... wat it should b doing is split the number then add digits and r until they r a single digit
MysticAngel




PostPosted: Tue Apr 15, 2003 6:24 pm   Post subject: (No subject)

How would i do the same thing for three or more numbers
code:


function Sumofsquares (n : int) : int
    var a, b, d : int := 0
    a := n mod 10
    b := n div 10
    d := a ** 2 + b ** 2
    result d
end Sumofsquares
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 3 of 4  [ 51 Posts ]
Goto page Previous  1, 2, 3, 4  Next
Jump to:   


Style:  
Search: