----------------------------------- MysticAngel Thu Mar 27, 2003 9:50 pm 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 ? :? ----------------------------------- Tony Thu Mar 27, 2003 10:07 pm ----------------------------------- 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 ----------------------------------- MysticAngel Sat Mar 29, 2003 7:16 pm ----------------------------------- 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. 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 ----------------------------------- Tony Sat Mar 29, 2003 7:45 pm ----------------------------------- 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? ----------------------------------- MysticAngel Sun Mar 30, 2003 12:25 am ----------------------------------- 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. :!: :) ----------------------------------- yuethomas Sun Mar 30, 2003 1:52 am ----------------------------------- All functions must return values. So, what you can do is to return a dummy value (that doesn't actually accomplish anything), like: 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). ----------------------------------- MysticAngel Sun Mar 30, 2003 2:30 am ----------------------------------- Dp u gave to sat like result = (something)? ----------------------------------- azndragon Sun Mar 30, 2003 9:49 am ----------------------------------- Okay, let me use an example from my game: 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. 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. ----------------------------------- MysticAngel Mon Mar 31, 2003 4:31 pm ----------------------------------- 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 ----------------------------------- MysticAngel Mon Mar 31, 2003 9:49 pm ----------------------------------- 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 ----------------------------------- Tony Mon Mar 31, 2003 10:47 pm ----------------------------------- this doesnt seem to work yet :? ----------------------------------- MysticAngel Mon Mar 31, 2003 11:16 pm ----------------------------------- I was supposed to attached the code to the previous reply. i forgot to. :oops: 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 ----------------------------------- Prince Tue Apr 01, 2003 11:38 am ----------------------------------- whoa, u got a shit load of errors there... ive got the same program at home (i think... i hope :?)... r u in the gr 11 comp sci class? ----------------------------------- Dan Tue Apr 01, 2003 4:41 pm ----------------------------------- 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 :? 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. ----------------------------------- MysticAngel Tue Apr 01, 2003 6:13 pm ----------------------------------- 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... :? 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 ----------------------------------- yuethomas Tue Apr 01, 2003 7:18 pm ----------------------------------- ... 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 var greatest, t, a, b, c : int c := getPositiveInteger a := c into var t, a, b : int a := m ----------------------------------- MysticAngel Wed Apr 02, 2003 6:06 pm ----------------------------------- i dont know why this isnt working ???? 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 ----------------------------------- Tony Wed Apr 02, 2003 6:19 pm ----------------------------------- 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 ----------------------------------- MysticAngel Wed Apr 02, 2003 8:20 pm ----------------------------------- 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 ----------------------------------- Prince Wed Apr 02, 2003 8:35 pm ----------------------------------- 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 ----------------------------------- MysticAngel Wed Apr 02, 2003 8:43 pm ----------------------------------- huh ????? :? ----------------------------------- MysticAngel Wed Apr 02, 2003 9:20 pm ----------------------------------- I dont understand what u said. how do i do that ----------------------------------- Prince Wed Apr 02, 2003 9:24 pm ----------------------------------- like this 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 function rFactorial (r : int) : real var R : real := 1 for j : 1 .. r R := R * j end for result R end rFactorial thats num2 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 :D) MOD Edit: thx for the help +10Bits - Tony ----------------------------------- MysticAngel Wed Apr 02, 2003 9:30 pm ----------------------------------- So when i call it somewhere i would call nrFactorial rite? ----------------------------------- Prince Wed Apr 02, 2003 9:45 pm ----------------------------------- thats rite ----------------------------------- yuethomas Wed Apr 02, 2003 9:56 pm ----------------------------------- Uh, a factorial can only result in an integer. No sense declaring it as a REAL function. ----------------------------------- Prince Wed Apr 02, 2003 10:14 pm ----------------------------------- 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... ----------------------------------- Tony Wed Apr 02, 2003 10:43 pm ----------------------------------- damn low level turing :evil: C++'s int variable is much larger is size :D ----------------------------------- MysticAngel Sat Apr 05, 2003 5:57 pm ----------------------------------- in a number theory, wat does the sum of the proper factors of a number mean. i mean how would you frame it. ----------------------------------- yuethomas Sun Apr 06, 2003 1:04 am ----------------------------------- 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. ----------------------------------- MysticAngel Sun Apr 06, 2003 10:13 am ----------------------------------- l how would u frame the code ? ----------------------------------- yuethomas Sun Apr 06, 2003 11:27 am ----------------------------------- Oh sorry, I misunderstood. I don't know of any algorithms, so I'll use a dumb one here... % 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 Mon Apr 07, 2003 10:02 am ----------------------------------- 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 :D ----------------------------------- Prince Mon Apr 07, 2003 10:04 am ----------------------------------- btw, yuethomas, proper factors do include 1 ----------------------------------- yuethomas Mon Apr 07, 2003 4:35 pm ----------------------------------- My mistake. ----------------------------------- MysticAngel Wed Apr 09, 2003 9:19 pm ----------------------------------- Can u plz check my code. It is supposed to give the reverse of a number. eg. 72--27 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 Wed Apr 09, 2003 9:27 pm ----------------------------------- I believe that was allready posted for reversing numbers, actually I guarantee it. Search the forums. ----------------------------------- MysticAngel Wed Apr 09, 2003 9:35 pm ----------------------------------- 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 :( 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 ----------------------------------- yuethomas Wed Apr 09, 2003 9:46 pm ----------------------------------- 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 Wed Apr 09, 2003 9:49 pm ----------------------------------- 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 :? first of all... to get the number of digits, you keep on dividing by multiples of 10 until your result is