Jump to content

find middle number/variable


petattlebaum

Recommended Posts

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!

 

 

Link to comment
https://forums.phpfreaks.com/topic/156985-find-middle-numbervariable/
Share on other sites

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?

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.

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));
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.