Computer Science Canada LowestCommonMultiple |
Author: | Viper [ Thu Nov 10, 2005 1:57 pm ] | ||
Post subject: | LowestCommonMultiple | ||
**Recursion must be used** The LCM (lowest common multiple ) of two non-zero integers can be found using the formula: lcm(num1, num2) = num1*num2/ gcd(num1, num2) where: gcd(num1, num2) is the greatest common divisor of num1 and num2. here's the trick. the program should employ a recursive approach to finding the gcd of two number. The recursive definition is given by: if num2 divides evenly into num1 then gcd= num2 could someone re-write tht in C++ for me plz ![]() here it is in turing
|
Author: | [Gandalf] [ Thu Nov 10, 2005 4:17 pm ] |
Post subject: | |
No. Try it yourself first. |
Author: | Geminias [ Fri Nov 11, 2005 6:12 pm ] | ||
Post subject: | hey | ||
here you go ![]() syntax = c++
|
Author: | wtd [ Fri Nov 11, 2005 6:25 pm ] | ||||
Post subject: | |||||
Wow...
Where do you think pNum1 and pNum2 are coming from? I don't see a declaration for them in the function. Nor do I see a declaration for them in the global scope.
You have this, which makes no sense, since you're missing an insertion operator. You don't have a closing brace at the end of "main". Is there any particular reason you thought this code would work? |
Author: | Geminias [ Fri Nov 11, 2005 6:36 pm ] |
Post subject: | |
yeah i honestly thought pointers would last forever until i deleted them lol i just tested and realized they don't... plus yeah there is a whole score of syntax errors |
Author: | wtd [ Fri Nov 11, 2005 6:39 pm ] |
Post subject: | |
Research the concept of "scope". |
Author: | Geminias [ Fri Nov 11, 2005 8:29 pm ] |
Post subject: | |
i think i understand scope but i misunderstood how a pointer works. i thought the specific purpose of a pointer was to access private data. Which, maybe it is, but i should declare the pointers as global. i'll check as soon as i have time |
Author: | wtd [ Fri Nov 11, 2005 8:36 pm ] |
Post subject: | |
Geminias wrote: i think i understand scope but i misunderstood how a pointer works. i thought the specific purpose of a pointer was to access private data. Which, maybe it is, but i should declare the pointers as global. i'll check as soon as i have time
No, you should not declare variables (a pointer is just a variable) globally. Learn how to pass things via arguments. |
Author: | Geminias [ Sat Nov 12, 2005 11:46 am ] | ||
Post subject: | heyho | ||
hey again, yeah i dropped out the idea of pointers and functions for this. it finds the lowest common multiple and the highest common multiple (LCM) (GCM) but it excludes 1. Because 1 divides into everything. in the case that only one number besides 1 is common to both numbers, then LCM = GCM. Viper, your recursion technique did not work. It does not find the LCM and GCM. try this.... (note: if one of the numbers is prime the program crashes.) syntax = c++
|
Author: | wtd [ Sat Nov 12, 2005 12:38 pm ] | ||||
Post subject: | |||||
What is "Greatest Common Multiple"? Infinity?
For a cleaned up version of your code.
|
Author: | wtd [ Sat Nov 12, 2005 12:55 pm ] | ||
Post subject: | |||
I'm not going to give you working C++ code for this, but I will show you the algorithm.
You may also wish to hit Google. Never know what you'll find. |
Author: | Geminias [ Sat Nov 12, 2005 1:35 pm ] |
Post subject: | |
i fixed the logic so it will return 1 as both lcm and gcm in the case that there are no other common multiples. hey wtd, how do you get all that pretty color in your code lol |
Author: | Andy [ Sat Nov 12, 2005 1:50 pm ] |
Post subject: | |
use code tags instead of [code ] use [sytax="cpp" ] |
Author: | Geminias [ Sat Nov 12, 2005 3:16 pm ] | ||
Post subject: | Re: heyho | ||
Geminias wrote: hey again, yeah i dropped out the idea of pointers and functions for this.
it finds the lowest common multiple and the highest common multiple (LCM) (GCM) but it excludes 1. Because 1 divides into everything. in the case that only one number besides 1 is common to both numbers, then LCM = GCM. Viper, your recursion technique did not work. It does not find the LCM and GCM. try this.... (note: if one of the numbers is prime the program crashes.) syntax = c++
|
Author: | Geminias [ Sat Nov 12, 2005 3:17 pm ] |
Post subject: | |
thanks ![]() |
Author: | [Gandalf] [ Sat Nov 12, 2005 4:04 pm ] |
Post subject: | |
So now you don't have to type syntax = c++ before your code, just do it inside the tags. You also don't have to double post because you can edit your posts. Even better, if you wanted to check if it works you could have used the preview button and edited your post before posting it ![]() |
Author: | Geminias [ Sun Nov 13, 2005 7:53 pm ] |
Post subject: | |
lol, ya ya, i'm a big noob. laugh while you still can ![]() |
Author: | wtd [ Sun Nov 13, 2005 8:06 pm ] |
Post subject: | |
We're all noobs next to someone. |
Author: | md [ Sun Nov 13, 2005 10:09 pm ] |
Post subject: | |
I disagree! I'm a noob next to everyone! Wait... In any case it's alright to make mistakes, so long as you learn from them ![]() |
Author: | Flikerator [ Tue Nov 15, 2005 3:01 pm ] |
Post subject: | |
Cornflake wrote: I disagree! I'm a noob next to everyone! Wait...
In any case it's alright to make mistakes, so long as you learn from them ![]() Except when sky diving...Its better to learn from other peoples mistakes ![]() ![]() |
Author: | bugzpodder [ Tue Nov 15, 2005 6:13 pm ] | ||
Post subject: | Re: LowestCommonMultiple | ||
Viper wrote: **Recursion must be used**
The LCM (lowest common multiple ) of two non-zero integers can be found using the formula: lcm(num1, num2) = num1*num2/ gcd(num1, num2) where: gcd(num1, num2) is the greatest common divisor of num1 and num2. here's the trick. the program should employ a recursive approach to finding the gcd of two number. The recursive definition is given by: if num2 divides evenly into num1 then gcd= num2 could someone re-write tht in C++ for me plz ![]() here it is in turing
gcd on reals? intriguing!! and I wouldnt do mod 0 if I were you. and be extra careful with negatives |
Author: | MysticVegeta [ Thu Jan 19, 2006 12:25 pm ] |
Post subject: | |
Wow the recursive solution to the GCD and LCM using GCD is amazing.. Never thought about it really.. really neat website wtd, saving a shortcut in my programming folder. |