Computer Science Canada

LCD,GCM

Author:  MysticAngel [ Thu Mar 27, 2003 9:50 pm ]
Post subject:  LCD,GCM

i wanted to know whether there was any posts on GCD and LCM. i think i saw it. but i browsed thorugh a few pages of the post but coudnt find it. did anyone had any post about GCD and LCM ? Confused

Author:  Tony [ Thu Mar 27, 2003 10:07 pm ]
Post subject: 

no, there were not posts about them before... but I've found a nice site explaining how to program your own algorythms on them

http://mathforum.org/epigone/k12.ed.math/flenzhendfling/jh1j3tg3l0o76tencfqnesiuh8g1cato04@4ax.com

Author:  MysticAngel [ Sat Mar 29, 2003 7:16 pm ]
Post subject: 

i dont know much about function. i saw the tutorials. but it didnt give me any help. I sort of did how much i understood. i know that there are lot of errors . i cant figure out how to get rid of them.

code:

function getpos () : int
var m : int
var n : int
put "Please enter a number"
get m
put ""
put "Please enter another number"
get n
end getpos

function GCD (n: int, m: int) : int
    var a, b, t : int
    getpos
    a:= m
    b := n
    loop
        t := a mod b
        exit when t = 0
        GCD := b
        a := b
        b := t
     end loop
end GCD
function LCM () : int
        getpos
        LCM (m, n) = (m + n) / gcd (m, n)
end LCM
function prime (n: int, m: int) : boolean
    GCD(m,n) = 1
end prime
getpos
put GCD
put LCM



Author:  Tony [ Sat Mar 29, 2003 7:45 pm ]
Post subject: 

function returns a value, so it must have result followed by a value. In your case, you want to use a procedure instead.

I'm sorry that tutorial didnt help you much... is there anything I can improve in it?

Author:  MysticAngel [ Sun Mar 30, 2003 12:25 am ]
Post subject: 

But i have to use functions. i cant use procedures yet. So can u check the code again and see if u tell me what is wrong
The tutorial can u a bit more example codes and a bit long ones like which use atleast 3 functions/ procedures (i guess) followed by what the program is doing - like what it the innput and the output stuff. Exclamation Smile

Author:  yuethomas [ Sun Mar 30, 2003 1:52 am ]
Post subject: 

All functions must return values. So, what you can do is to return a dummy value (that doesn't actually accomplish anything), like:

code:
function a (b : int) : int

...

    result 0
end a

What you attach after result is really up to you. Conventionally, it is put as 0 or 1 (to indicate success) and -1 (to indicate failure).

Author:  MysticAngel [ Sun Mar 30, 2003 2:30 am ]
Post subject: 

Dp u gave to sat like result = (something)?

Author:  azndragon [ Sun Mar 30, 2003 9:49 am ]
Post subject: 

Okay, let me use an example from my game:

code:
function caps (word : string) : string                  %Converts a word into pure caps, so that the inputs can be more easily recognized.
    word2 := ""
    for i : 1 .. length (word)
        if ord (word (i .. i)) > 96 then
            if ord (word (i .. i)) < 123 then
                word2 := word2 + chr (ord (word (i)) - 32)
            else
                word2 := word2 + chr (ord (word (i)) + 0)
            end if
        else
            word2 := word2 + chr (ord (word (i)) + 0)
        end if
    end for
    result word2
end caps


First thing is the function name. That should be pretty simple. The variable in the brackets is the text that you input when you call the function. If you use the code:

caps(randomword)

The value stored in randomword will be sent to the function under the variable name of word. The middle section of the function can pretty much be ignored, since you can change it to whatever you want to do. However, notice that in the function, a new variable, called word2 is being created, based on the word variable. At the end of the function is "result word2" This is the final result of function. This will be set as a new variable if you call it sometime later. Ex.

code:

var someword : string
var newword : string

put "Enter a word"
get someword

newword := caps (someword)

put newword


Lets say the user enters the word "Hello". The function will run through it's course, and word2 will now be "HELLO". The result is given, and the final output of the program is "HELLO". Hope this helps.

Author:  MysticAngel [ Mon Mar 31, 2003 4:31 pm ]
Post subject: 

k sort og know what functions are and how they work and stuff. but i am not sure whether my code is ok or not. plz check it, cuz there are a few errors and i cant figure them out. plz tell me what are are the mistakes/ solutions

Author:  MysticAngel [ Mon Mar 31, 2003 9:49 pm ]
Post subject: 

code:

function getPositiveInteger : int
    var n : int
    loop
        get n
        exit when n > 0
        put " A positive value please :"
    end loop
    result n
end getPositiveInteger
%------------------------------------------------------
function GreatestCommonDivisor (n : int) : int
    var greatest,t, a , b,m : int
    m:= getPositiveInteger
        a := m
        b := n
        loop   
            t := a mod b
            exit when t < 1           
            a:= b
            b:= t
            result b
        end loop
end GreatestCommonDivisor
%------------------------------------------------------   
function LeastCommonMultiple (m,n : int) :int
    var lcm : int
    lcm := (m*n) / GreatestCommonDivisor(m,n)
    result lcm
end LeastCommonMultiple
%-----------------------------------------------------
function relativelyprime(m,n : int) : boolean
    if GreatestCommonDivisor = 1 then
        relativelyprime = true
    else
        relativelyprime = false
    end if
    result relativelyprime (m,n)
end relativelyprime
   
%-----------------------------------------------------------
var num1, num2,a1, b1 : int
var c1 : boolean
loop
put "Please enter a number"
    num1:= getPositiveInteger (n)
put "Please enter another number"
    num2 := getPositiveInteger(m)
put "GCD = "
    a1:= GreatestCommonDivisor
    put a1
put "LCM = "
    b1 = LeastCommonMultiple (m,n)
    put b1
 put "Relatively prime"
    c1 := relativelyprime
    put c1
end loop


Author:  Tony [ Mon Mar 31, 2003 10:47 pm ]
Post subject: 

this doesnt seem to work yet Confused

Author:  MysticAngel [ Mon Mar 31, 2003 11:16 pm ]
Post subject: 

I was supposed to attached the code to the previous reply. i forgot to. Embarassed
k sort og know what functions are and how they work and stuff. but i am not sure whether my code is ok or not. plz check it, cuz there are a few errors and i cant figure them out. plz tell me what are are the mistakes/ solutions

[/code]
function getPositiveInteger : int
var n : int
loop
get n
exit when n > 0
put " A positive value please :"
end loop
result n
end getPositiveInteger
%------------------------------------------------------
function GreatestCommonDivisor (n : int) : int
var greatest,t, a , b,m : int
m:= getPositiveInteger
a := m
b := n
loop
t := a mod b
exit when t < 1
a:= b
b:= t
result b
end loop
end GreatestCommonDivisor
%------------------------------------------------------
function LeastCommonMultiple (m,n : int) :int
var lcm : int
lcm := (m*n) / GreatestCommonDivisor(m,n)
result lcm
end LeastCommonMultiple
%-----------------------------------------------------
function relativelyprime(m,n : int) : boolean
if GreatestCommonDivisor = 1 then
relativelyprime = true
else
relativelyprime = false
end if
result relativelyprime (m,n)
end relativelyprime

%-----------------------------------------------------------
var num1, num2,a1, b1 : int
var c1 : boolean
loop
put "Please enter a number"
num1:= getPositiveInteger (n)
put "Please enter another number"
num2 := getPositiveInteger(m)
put "GCD = "
a1:= GreatestCommonDivisor
put a1
put "LCM = "
b1 = LeastCommonMultiple (m,n)
put b1
put "Relatively prime"
c1 := relativelyprime
put c1
end loop

Author:  Prince [ Tue Apr 01, 2003 11:38 am ]
Post subject: 

whoa, u got a shit load of errors there... ive got the same program at home (i think... i hope Confused)... r u in the gr 11 comp sci class?

Author:  Dan [ Tue Apr 01, 2003 4:41 pm ]
Post subject: 

The function GreatestCommonDivisor only takes one number but it is beiing called with 2. also you some times calling it with no numbers wich dose not wrok.

also in turing you use := not = when asing a vlaue you messed that up a few times Confused

then you send a vlaue, n to the a fuction that has no vlaue and you do not input the users repose to put in a number.

Author:  MysticAngel [ Tue Apr 01, 2003 6:13 pm ]
Post subject: 

yes i am in grade 11. neways i cahnged some stuff and it works now but theere are some problems in the output. k when u run the code. it works correctly, but after u enter the two numbers, u have to enter another number then u would get the GCD and then again u would have to enter another number even when it is not asking for then you would get the other results... Confused

code:

function getPositiveInteger : int
    var n : int
    loop
        get n
        exit when n > 0
    end loop
    result n
end getPositiveInteger
%------------------------------------------------
function GreatestCommonDivisor (m, n : int) : int
    var greatest, t, a, b, c : int
    c := getPositiveInteger
    a := c
    b := n
    loop
        t := a mod b
        exit when t < 1
        a := b
        b := t
        result b
    end loop
end GreatestCommonDivisor
%------------------------------------------------
function LeastCommonMultiple (m, n : int) : int
    var lcm : int
    lcm := (m * n) div GreatestCommonDivisor (m, n)
    result lcm
end LeastCommonMultiple
%------------------------------------------------
function relativelyprime (m, n : int) : boolean
    var bool : boolean := false
    if GreatestCommonDivisor (m, n) = 1 then
        bool := true
    end if
    result bool
end relativelyprime

%-----------------------------------------------------------
var num1, num2, a1, b1 : int
var c1 : boolean
loop
    put "Please enter a number"
    num1 := getPositiveInteger
    put "Please enter another number"
    num2 := getPositiveInteger
    a1 := GreatestCommonDivisor (num1, num2)
    put "GCD = ", a1
    b1 := LeastCommonMultiple (num1, num2)
    put "LCM = ", b1
    c1 := relativelyprime (num1, num2)
    put "Relatively prime", c1
end loop

Author:  yuethomas [ Tue Apr 01, 2003 7:18 pm ]
Post subject: 

code:

...
    c := getPositiveInteger
...


This line prompts you to enter another number for C. As I am not familiar with the algorithm for calculating the GCM of two numbers, I have no idea what it's for.

Update:

After analyzing the algorithm, I think it's perfectly safe to change
code:
    var greatest, t, a, b, c : int
    c := getPositiveInteger
    a := c

into
code:
    var t, a, b : int
    a := m

Author:  MysticAngel [ Wed Apr 02, 2003 6:06 pm ]
Post subject: 

i dont know why this isnt working ????
code:

function getPositiveInteger : int
    var n : int
    loop
        get n
        exit when n > 0
        put "Positive interger please  " ..
    end loop
    result n
end getPositiveInteger
%--------------------------------------------
function Factorial(n: int): real
    var count, count1 : int
    loop
        count := count + 1
        count1 := count1 * count
        exit when count = n
        end loop
    result count1
end Factorial
%-----------------------------------------
function Permutation (n, r : int) : real   
    var p : int
    p := Factorial(n) div (n - r)
    result p
end Permutation
%----------------------------------------
function Combination (n, r : int) : real
    var c : int
    c := Factorial(n)  div r*(n-r)
    result c
end Combination
%------------------*Main Program-------------------------
var num1, num2, a, b : int
put "Please enter a positive number"
num1 := getPositiveInteger
put "Please enter another positve number"
num2 := getPositiveInteger
if num1 >= num2 then
    a := Permutation(num1,num2)
    put a
    b :=Combination (num1,num2)
    put b
end if


Author:  Tony [ Wed Apr 02, 2003 6:19 pm ]
Post subject: 

thats because your functions return real values, but you try to assign them to integer varaibles.

declear a,b as real and it should work

Author:  MysticAngel [ Wed Apr 02, 2003 8:20 pm ]
Post subject: 

is this correct. like this is n!. if u input 5 the answer shuld be (5! = 1*2*3*4*5) = 120


[/code]
function Factorial(n: int): real
var count,e : int
count := 1
loop
count := count + 1
e := 1 * count
exit when count = n
end loop
result n
end Factorial

Author:  Prince [ Wed Apr 02, 2003 8:35 pm ]
Post subject: 

try splitting the factorial function so ur finding the factorial of num1, num2 and num1 - num2... finding the permutations and combinations should b easier then

Author:  MysticAngel [ Wed Apr 02, 2003 8:43 pm ]
Post subject: 

huh ????? Confused

Author:  MysticAngel [ Wed Apr 02, 2003 9:20 pm ]
Post subject: 

I dont understand what u said. how do i do that

Author:  Prince [ Wed Apr 02, 2003 9:24 pm ]
Post subject: 

like this

code:

function nFactorial (n : int) : real
    var N : real := 1
    for j : 1 .. n
        N := N * j
    end for
    result N
end nFactorial


thats for num1

code:

function rFactorial (r : int) : real
    var R : real := 1
    for j : 1 .. r
        R := R * j
    end for
    result R
end rFactorial


thats num2

code:

function nrFactorial (var n, r : int) : real
    var NR : real := 1
    for j : 1 .. n - r
        NR := NR * j
    end for
    result NR
end nrFactorial


and that one is num1 - num2... it may seem confusing (ok it really is) but its really not that hard (especially since i did this last year Very Happy)

MOD Edit: thx for the help +10Bits - Tony

Author:  MysticAngel [ Wed Apr 02, 2003 9:30 pm ]
Post subject: 

So when i call it somewhere i would call nrFactorial rite?

Author:  Prince [ Wed Apr 02, 2003 9:45 pm ]
Post subject: 

thats rite

Author:  yuethomas [ Wed Apr 02, 2003 9:56 pm ]
Post subject: 

Uh, a factorial can only result in an integer. No sense declaring it as a REAL function.

Author:  Prince [ Wed Apr 02, 2003 10:14 pm ]
Post subject: 

test any one of those functions with a value over 10 and u'll see y it has to b REAL... did u do it?? ok then...

Author:  Tony [ Wed Apr 02, 2003 10:43 pm ]
Post subject: 

damn low level turing Evil or Very Mad C++'s int variable is much larger is size Very Happy

Author:  MysticAngel [ Sat Apr 05, 2003 5:57 pm ]
Post subject: 

in a number theory, wat does the sum of the proper factors of a number mean. i mean how would you frame it.

Author:  yuethomas [ Sun Apr 06, 2003 1:04 am ]
Post subject: 

Proper factors of a number is the factors of that number except 1 and the number itself. For example, the proper factors of 30 are 2, 3, 5, 6, 10, and 15.

So the sum of the proper factors of 30 would be 2+3+5+6+10+15=31.

Author:  MysticAngel [ Sun Apr 06, 2003 10:13 am ]
Post subject: 

l how would u frame the code ?

Author:  yuethomas [ Sun Apr 06, 2003 11:27 am ]
Post 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

Author:  Prince [ Mon Apr 07, 2003 10:02 am ]
Post 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

Author:  Prince [ Mon Apr 07, 2003 10:04 am ]
Post subject: 

btw, yuethomas, proper factors do include 1

Author:  yuethomas [ Mon Apr 07, 2003 4:35 pm ]
Post subject: 

My mistake.

Author:  MysticAngel [ Wed Apr 09, 2003 9:19 pm ]
Post 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

Author:  Asok [ Wed Apr 09, 2003 9:27 pm ]
Post subject: 

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

Author:  MysticAngel [ Wed Apr 09, 2003 9:35 pm ]
Post 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

Author:  yuethomas [ Wed Apr 09, 2003 9:46 pm ]
Post 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)

Author:  Tony [ Wed Apr 09, 2003 9:49 pm ]
Post 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

Author:  MysticAngel [ Wed Apr 09, 2003 9:55 pm ]
Post subject: 

it works the way he said

Author:  MysticAngel [ Thu Apr 10, 2003 9:59 pm ]
Post 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

Author:  Prince [ Thu Apr 10, 2003 10:14 pm ]
Post 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

Author:  Prince [ Fri Apr 11, 2003 10:08 am ]
Post 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

Author:  MysticAngel [ Tue Apr 15, 2003 6:24 pm ]
Post 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

Author:  Prince [ Tue Apr 15, 2003 6:37 pm ]
Post subject: 

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

Author:  Prince [ Tue Apr 15, 2003 6:40 pm ]
Post subject: 

unless ur talkin about a different function

Author:  MysticAngel [ Tue Apr 15, 2003 7:45 pm ]
Post 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

Author:  MysticAngel [ Tue Apr 15, 2003 8:02 pm ]
Post 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

Author:  Prince [ Tue Apr 15, 2003 9:41 pm ]
Post 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

Author:  Prince [ Wed Apr 16, 2003 10:45 am ]
Post 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???


: