petattlebaum Posted May 5, 2009 Share Posted May 5, 2009 I'm a newbie taking a class working on this problem. Which I've got up until the last part. I've got a form with 3 fields; all numbers. So, I type in 3 numbers and then the form is sent to a php page. Here's where my issue comes in; using a loop, I need to calculate the least comon denominator between the middle number and largest number. I know how to get the largest number; $largestNum = (max($num1, $num2, $num3)); Where I'm stumped is how to get the middle number. I think I know the logic but can't figure out how to implement it. Here's what I think I need to do: Create a While loop that finds the largest and smallest variables, and then use the one that is NOT them. Then make it a variable and multiply it times the max value ($largestNUm from above). Am I on the right track? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/156985-find-middle-numbervariable/ Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Okay so you need the least common denominator as the end result right? But if you're getting that, then I'm going to assume you're going to be doing division. So why not just let math take care of itself? It really doesn't matter what you do as long as you get a common denominator and multiply the numerator respectively because that's how division works. So just multiply all three of those numbers together and have math take care of tiself. Unless your purpose is to determine the least common denominator? Quote Link to comment https://forums.phpfreaks.com/topic/156985-find-middle-numbervariable/#findComment-826962 Share on other sites More sharing options...
petattlebaum Posted May 5, 2009 Author Share Posted May 5, 2009 Yes, I got ahead of myself. I THOUGHT I knew what the least common denominator was. I do now but don't know how to calculate it. Quote Link to comment https://forums.phpfreaks.com/topic/156985-find-middle-numbervariable/#findComment-826971 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Well it's a bit hard with 3 numbers. With 2 numbers it's already a bit hard, but there is a way that involves using the GCD's Euclidean Algorithm. I studied that in Logic and Computations. Which is why I asked what you were doing. If you were doing division, just multiply all 3 numbers. Quote Link to comment https://forums.phpfreaks.com/topic/156985-find-middle-numbervariable/#findComment-826978 Share on other sites More sharing options...
petattlebaum Posted May 5, 2009 Author Share Posted May 5, 2009 My bad again. To calculate the lcd, I am only using 3 numbers; the highest and middle numbers. Quote Link to comment https://forums.phpfreaks.com/topic/156985-find-middle-numbervariable/#findComment-826987 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Let's take this example: 4,5,6 Lowest common denominator is 60, right? Using the middle and highest numbers (5 and 6 respectively), the lowest common denominator is 30. How are you doing this with those two numbers? I'm interested to know the formula. Quote Link to comment https://forums.phpfreaks.com/topic/156985-find-middle-numbervariable/#findComment-827000 Share on other sites More sharing options...
sasa Posted May 6, 2009 Share Posted May 6, 2009 lcd(a, b) * gcd(a, b) = a * b <?php function gcd($a, $b){ if ($a < 1 or $b < 1 or $a != (int) $a or $b != (int) $b) return false; while ($r = $a % $b){ $a = $b; $b = $r; } return $b; } function lcd($a, $b){ if ($gcd = gcd($a, $b)) return $a * $b / $gcd; else return false; } function lcd_a($array){ if (count($array) == 2) return lcd(array_pop($array), array_pop($array)); $a = array_pop($array); return lcd(lcd_a($array), $a); } echo lcd(5,6) echo lcd_a(array(4,5,6)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/156985-find-middle-numbervariable/#findComment-827230 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.