Jump to content

PHP & math


spfoonnewb

Recommended Posts

the easiest way would be to just use two different functions: one to find its multiplicative factors, one to imply iterate through every additive combination:

 

<?php

function get_additives($number)
{
  // setup the array of results
  $combos = array();

  // get the halfway mark (all combos above this will be degenerate)
  $halfsies = floor($number / 2);

  // get the additive combos for up to half the number
  for ($i = 0; $i <= $halfsies; ++$i)
  {
    $combos[] = array($i, ($number - $i));
  }

  // return the results
  return $combos;
}

// echo the additives of 10
$additives = get_additives(10);
foreach ($additives AS $arr)
{
  echo $arr[0].' + '.$arr[1].'<br />';
}

?>

 

as for grabbing the factors, i'm too lazy to write one for that right now.  i'm sure there's a function online somewhere to do that for you.  to be honest, this isn't PHP so much as simple math written with PHP to return the results.

Link to comment
https://forums.phpfreaks.com/topic/45961-php-math/#findComment-223279
Share on other sites

that would be far more effort and would require recursion most likely.  you'd need to whittle down your selectivity for that to happen - can you imagine how many possible combinations there are for 500 if you didn't restrict the minimum additive size?

 

EDIT:  what are you trying to do?  there might be a more efficient way to accomplish what you're trying to do in the long run using these factors.

Link to comment
https://forums.phpfreaks.com/topic/45961-php-math/#findComment-223293
Share on other sites

"can you imagine how many possible combinations there are for 500 if you didn't restrict the minimum additive size" is exactly what I wanted. It's a problem I found, and theres more to it, I just need to get the numbers to do the rest.. I hope.

 

Anyways.. and suggestion for where to start?

Link to comment
https://forums.phpfreaks.com/topic/45961-php-math/#findComment-223302
Share on other sites

So I figured out how to make it use multiple numbers.. but how can I make it recursive, rather than having to add more things..

<?php

$max = 10;

function get_additives($number) {
$combos = array();
$halfsies = floor($number / 2);

for ($i = 1; $i <= $halfsies; ++$i) {
  		
  		for ($i2 = 1; $i2 <= $halfsies; ++$i2) {
  		
  			for ($i3 = 1; $i3 <= $halfsies; ++$i3) {
  				
  				if ($i + $i + $i2 + $i2 + $i3 + $i3 == $number) {
    				$combos[] = array($i, $i, $i2, $i2, $i3, $i3);
  				}
  			}
  		}
  	}
return $combos;
}

$additives = get_additives($max);

foreach ($additives AS $arr) {
  echo "".$arr[0]." + ".$arr[1]." + ".$arr[2]." + ".$arr[3]." + ".$arr[4]." + ".$arr[5]." = $max<br />\n";
}

?>

 

Currently outputs:

 

1 + 1 + 1 + 1 + 3 + 3 = 10

1 + 1 + 2 + 2 + 2 + 2 = 10

1 + 1 + 3 + 3 + 1 + 1 = 10

2 + 2 + 1 + 1 + 2 + 2 = 10

2 + 2 + 2 + 2 + 1 + 1 = 10

3 + 3 + 1 + 1 + 1 + 1 = 10

Link to comment
https://forums.phpfreaks.com/topic/45961-php-math/#findComment-223597
Share on other sites

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.