Jump to content

Number combination calculation


Readme

Recommended Posts

I am trying to find a calculation that will determine the number of two digit combinations that can be derived from twelve numbers, and what those number are. Any thoughts?

 

Example: 68 41 90 100 are the numbers.

 

68  41 

68  90 

68  100 

41  68 

41  90

41  100

90  68

90  41

90  100

100  68

100  41

100  90

 

Thanks!!!

 

Link to comment
Share on other sites

Good job!! But

 

Result (incorrect): 68 4168 9068 10041 6841 9041 10090 6890 4190 100100 68100 41100 90 

 

correct 100%

68  41 

68  90 

68  100 

41  68 

41  90

41  100

90  68

90  41

90  100

100  68

100  41

100  90

 

 

my English is not very good

Link to comment
Share on other sites

It is correct, I just didn't put a line break tag in it:

<?php 
  $nums = array(68,41,90,100);
  foreach($nums as $i=>$num1){
    foreach($nums as $j=>$num2){
      if($i == $j) continue;
      print "$num1 $num2<br>";
    }
  }
?>

Link to comment
Share on other sites

so something like this?

 

<?php 
  function getCombos ( $arr, $n ,$stack = array(), $combos = array()){
    if(count($stack) < $n){
      foreach(array_keys($arr) as $key){
        if(in_array($key,array_keys($stack))) continue;
        $combos = getCombos($arr,$n,array_merge($stack,array($key=>$arr[$key])),$combos);
      }
    }else
      $combos[] = array_values($stack);
    return $combos;
  }

  $nums = array(68,41,90,100,32,56,78,34,23,53);
  $combos = getCombos($nums,3);
  foreach($combos as $combo)
    print implode(' ',$combo)."<br>";
?>

Link to comment
Share on other sites

How do!

 

When creating a combination of numbers, the number sequence becomes too long, and to avoid this, you need to bring in an exact number (68).

Example:

 

$number = array([68],41,90,100,32,56

[68] is exact number

 

68 41 90 100 32 56

68 41 90 100 56 56

68 41 90 32 32 56

68 41 90 32 56 56

68 41 90 56 32 56

68 41 90 56 56 56

Link to comment
Share on other sites

$nums = array(68,41,90,100,32,56)

 

That it takes the first number (68) and creates a combination with other numbers; then further number 41 and creates new combination, etcetera. I would need it this way, that by giving it a number, the system would create a combination with other numbers.

 

 

 

My English is not very good

 

Link to comment
Share on other sites

I think I see what you are asking. Just leave the first number off the front of the array, and add it back when printing:

 

<?php 
  function getCombos ( $arr, $n ,$stack = array(), $combos = array()){
    if(count($stack) < $n){
      foreach(array_keys($arr) as $key){
        if(in_array($key,array_keys($stack))) continue;
        $combos = getCombos($arr,$n,array_merge($stack,array($key=>$arr[$key])),$combos);
      }
    }else
      $combos[] = array_values($stack);
    return $combos;
  }

  $base = 68;
  $nums = array(41,90,100,32,56);
  $combos = getCombos($nums,count($nums));
  foreach($combos as $combo)
    print $base." ".implode(' ',$combo)."<br>";
?>

 

output:

68 41 90 100 32 56
68 41 90 100 56 56
68 41 90 32 32 56
68 41 90 32 56 56
68 41 90 56 32 56
68 41 90 56 56 56
68 41 100 100 32 56
68 41 100 100 56 56
68 41 100 32 32 56
68 41 100 32 56 56
68 41 100 56 32 56
68 41 100 56 56 56
68 41 32 100 32 56
68 41 32 100 56 56
68 41 32 32 32 56
68 41 32 32 56 56
68 41 32 56 32 56
68 41 32 56 56 56
68 41 56 100 32 56
68 41 56 100 56 56
68 41 56 32 32 56
68 41 56 32 56 56
68 41 56 56 32 56
68 41 56 56 56 56
68 90 90 100 32 56
68 90 90 100 56 56
68 90 90 32 32 56
68 90 90 32 56 56
68 90 90 56 32 56
68 90 90 56 56 56
68 90 100 100 32 56
68 90 100 100 56 56
68 90 100 32 32 56
68 90 100 32 56 56
68 90 100 56 32 56
68 90 100 56 56 56
68 90 32 100 32 56
68 90 32 100 56 56
68 90 32 32 32 56
68 90 32 32 56 56
68 90 32 56 32 56
68 90 32 56 56 56
68 90 56 100 32 56
68 90 56 100 56 56
68 90 56 32 32 56
68 90 56 32 56 56
68 90 56 56 32 56
68 90 56 56 56 56
68 100 90 100 32 56
68 100 90 100 56 56
68 100 90 32 32 56
68 100 90 32 56 56
68 100 90 56 32 56
68 100 90 56 56 56
68 100 100 100 32 56
68 100 100 100 56 56
68 100 100 32 32 56
68 100 100 32 56 56
68 100 100 56 32 56
68 100 100 56 56 56
68 100 32 100 32 56
68 100 32 100 56 56
68 100 32 32 32 56
68 100 32 32 56 56
68 100 32 56 32 56
68 100 32 56 56 56
68 100 56 100 32 56
68 100 56 100 56 56
68 100 56 32 32 56
68 100 56 32 56 56
68 100 56 56 32 56
68 100 56 56 56 56
68 32 90 100 32 56
68 32 90 100 56 56
68 32 90 32 32 56
68 32 90 32 56 56
68 32 90 56 32 56
68 32 90 56 56 56
68 32 100 100 32 56
68 32 100 100 56 56
68 32 100 32 32 56
68 32 100 32 56 56
68 32 100 56 32 56
68 32 100 56 56 56
68 32 32 100 32 56
68 32 32 100 56 56
68 32 32 32 32 56
68 32 32 32 56 56
68 32 32 56 32 56
68 32 32 56 56 56
68 32 56 100 32 56
68 32 56 100 56 56
68 32 56 32 32 56
68 32 56 32 56 56
68 32 56 56 32 56
68 32 56 56 56 56
68 56 90 100 32 56
68 56 90 100 56 56
68 56 90 32 32 56
68 56 90 32 56 56
68 56 90 56 32 56
68 56 90 56 56 56
68 56 100 100 32 56
68 56 100 100 56 56
68 56 100 32 32 56
68 56 100 32 56 56
68 56 100 56 32 56
68 56 100 56 56 56
68 56 32 100 32 56
68 56 32 100 56 56
68 56 32 32 32 56
68 56 32 32 56 56
68 56 32 56 32 56
68 56 32 56 56 56
68 56 56 100 32 56
68 56 56 100 56 56
68 56 56 32 32 56
68 56 56 32 56 56
68 56 56 56 32 56
68 56 56 56 56 56

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.