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
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prince




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

the code i put up b4 should do it for more than 2 digits... if it doesnt, tell me
Sponsor
Sponsor
Sponsor
sponsor
Prince




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

unless ur talkin about a different function
MysticAngel




PostPosted: Tue Apr 15, 2003 7:45 pm   Post subject: (No subject)

i meant that the code what i had works for only like two digits that is eneterd. what i am trying to do it to make it like work for more than two digits.
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
MysticAngel




PostPosted: Tue Apr 15, 2003 8:02 pm   Post subject: (No subject)

cuz when i call it here. it gives me an over flow erroe at a**2 + b**2
here is the code again
code:

function Sumoffactors (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 Sumoffactors
function prime (n : int) : boolean
    var p : boolean := false
    if Sumoffactors (n) = 1 then
        p := true
    end if
    result p
end prime
%********************
var c : boolean
put "Primes :"
put ""
for l : 1 .. 5
    c := prime (l)
    if c = true then
        put l, " " ..
    end if
end for
Prince




PostPosted: Tue Apr 15, 2003 9:41 pm   Post subject: (No subject)

ok here r a few things that can help u with ur program...

1. remember the KISS principle.. ur doin too much in ur prime function, lessen the load a bit

code:

function prime (n : int) : boolean
    if n = 1 then
        result true
    else
        result false
    end if
end prime


2. wen ur checkin for prime numbers put it in a for loop like so

code:

for j : 1 .. 100 by 1
    var y : int
    y := properFactors (j)
    if y = 1 then
        put j : 3 ..
    end if
end for


now tie it all together and this is wat chu get for that part of the program
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
% **************
for j : 1 .. 100 by 1
    var y : int
    y := properFactors (j)
    if y = 1 then
        put j : 3 ..
    end if
end for


u dont really need the prime function here (uve got it up top anyway) so i took it out incase it may confuse u... but there's ur solution all nice and simple like
Prince




PostPosted: Wed Apr 16, 2003 10:45 am   Post subject: (No subject)

k mayb its jus me but wenever i reread wat i write in this thread i get confused... r we talkin bout the same functions and procedures now???
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 4 of 4  [ 51 Posts ]
Goto page Previous  1, 2, 3, 4
Jump to:   


Style:  
Search: