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 ? |
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.
|
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. |
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:
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:
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.
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: | |||
|
Author: | Tony [ Mon Mar 31, 2003 10:47 pm ] |
Post subject: | |
this doesnt seem to work yet |
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. 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 )... 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 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...
|
Author: | yuethomas [ Tue Apr 01, 2003 7:18 pm ] | ||||||
Post subject: | |||||||
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
into
|
Author: | MysticAngel [ Wed Apr 02, 2003 6:06 pm ] | ||
Post subject: | |||
i dont know why this isnt working ????
|
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 ????? |
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
thats for num1
thats num2
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 ) 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 C++'s int variable is much larger is size |
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...
|
Author: | Prince [ Mon Apr 07, 2003 10:02 am ] | ||
Post subject: | |||
now doesnt that seem a whole lot simpler... it works too |
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
|
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
|
Author: | yuethomas [ Wed Apr 09, 2003 9:46 pm ] | ||
Post subject: | |||
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 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 |
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 ????
|
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
im no good at explainin things so im not gonna start here (even tho its my code )... 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:
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
|
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.
|
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
|
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
2. wen ur checkin for prime numbers put it in a for loop like so
now tie it all together and this is wat chu get for that part of the program
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??? |