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

Username:   Password: 
 RegisterRegister   
 Help -Function get Prime fail to give a result
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SilverMan




PostPosted: Wed Oct 31, 2007 10:38 pm   Post subject: Help -Function get Prime fail to give a result

Hi . i am new in this forum and i have some little problem with my function program. My get Prime function does work but it will report Function failed to give a result when it hit 100. Also i think other part of my function may be not working either- can someone point out some error and how can it fix it !!!.

code:

function getPositiveInteger : int
    const ENTER : char := chr (10) % Constant ASCII code for the Enter Key
    var ch : string (1) := 'x'
    var number : int := 0
    var digits : int := 0

    put "Please enter a positive Integer : " ..

    loop
        if hasch then
            getch (ch)

            if ch >= '0' and ch <= '9' then
                number := number * 10 + strint (ch)
                digits := digits + 1
            elsif ch not= ENTER then
                digits := 0
                number := 0
                cls
                put "** Invalid Entry Attempted**"
                put
                    "Please enter an Integer greater than or equal to zero: "
                    ..
            end if

        end if

        exit when ch = chr (10) or digits >= 9
    end loop

    result number
end getPositiveInteger
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getProperFactors (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
            else
                exit when i = 100
            end if
        end if
    end for

    result temp
end getProperFactors
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getDigitalRoot (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 getDigitalRoot
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getReverse (n : int) : int
    var reverse : int := 0
    var r : int := n

    loop
        reverse := reverse * 10 + (r mod 10)
        r := r div 10
        exit when r = 0
    end loop

    result reverse
end getReverse
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getPrime (n : int) : int
    for j : 1 .. 100 by 1
        var y : int
        y := getProperFactors (j)
        if y = 1 then
            put j : 3 ..
        end if
    end for
end getPrime
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
function palindrome (n : int) : boolean

    if n = getReverse (n) then
        result true
    else
        result false
    end if

end palindrome
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function checkPrime (n : int) : boolean
    for c : 2 .. (n - 1)
        if (n mod c) = 0 then
            result false
        end if
    end for

    result true
end checkPrime
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getPerfect (n : int) : real
    var sum : int := 0

    for i : 1 .. round (n / 2)
        if n mod i = 0 then
            sum := sum + i
        end if
    end for

    result n
end getPerfect
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function checkPerfect (n : int) : boolean
    var sum : int
    if n = getPerfect (sum) then
        result true
    else
        result false
    end if
end checkPerfect

%~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getHappy (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 getHappy
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function checkHappy (n : int) : boolean
    var x, y : int := 0
    for increasing : n .. 15 by 1
        if getHappy (n) < 15 then
            result true
        else
            result false
        end if
    end for
end checkHappy
%~~~~~~~~~~~~~~~~~~Main Program~~~~~~~~~~~~~~~~~~~~~~~
var a, b, c : real := 0
var count : int := 0
var n : int := 0

put " Prime number are: ", a
loop
    a := getPrime (n)
    count := count + 1
    exit when count = 100
end loop

put " Perfect number are: ", b
loop
    b := getPerfect (n)
    count := count + 1
    exit when count = 10000
end loop

put " Happy number are: ", c
loop
    c := getHappy (n)
    count := count + 1
    exit when count = 500
end loop

var num : int
put "Please enter a positive number: " ..
num := getPositiveInteger

var a1, b1, c1 : int
var a2, b2, c2 : boolean

a1 := getReverse (num)
put " The reverse of the number is: " ..

b1 := getProperFactors (num)
put " The proper factors of the number is: " ..

c1 := getDigitalRoot (num)
put " The digital root is: " ..

a2 := checkPrime (num)
put "Prime ", a2

b2 := checkPerfect (num)
put "Perfect ", b2

c2 := checkHappy (num)
put " Happy ", c2
Sponsor
Sponsor
Sponsor
sponsor
Nick




PostPosted: Wed Oct 31, 2007 10:54 pm   Post subject: RE:Help -Function get Prime fail to give a result

Turing:
function getPrime (n : int) : int
for j : 1 .. 100 by 1
var y : int
y := getProperFactors (j)
if y = 1 then
put j : 3 ..
end if
end for
end getPrime

there is no resulted value :s

for refrence check out my prime factor detector
also found a procedures and funtions tuturial

one last link from me, code tags
CodeMonkey2000




PostPosted: Thu Nov 01, 2007 10:27 am   Post subject: RE:Help -Function get Prime fail to give a result

There is an easier way to deterim prime nubers. this is thew basic concept. Start with 2. You know that 2 is a prime so eliminate all multiples of 2. Go to the next number (three). Three is prime so eliminate all multiples of 3 (since they are not prime). Go to the next number which is 4. Four is not prime since it was eliminated by 2 earlier. Go on to 5. Five is prime because it wasn't eliminated yet, now eliminate all multiples of 5.
SilverMan




PostPosted: Thu Nov 01, 2007 3:46 pm   Post subject: RE:Help -Function get Prime fail to give a result

thx momop . You help me solve the problem T__T. forget to state the result there
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: