Talk:Main Page

From Wiki-UX.info
Jump to: navigation, search

use strict;

  1. Note that 0 is a valid combination, because you can decide to take a rest any other day
  2. As a matter of fact, that is the trick, because you are never told that you have to kill a cow every day

my @odd_list = ( 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49 ); my @test_group;

my $eval_count = 1; my $sucess_count = 1; my %unique_combinations;

for my $index0 ( @odd_list ) {

  for my $index1 ( @odd_list ) {
     for my $index2 ( @odd_list ) {
        for my $index3 ( @odd_list ) {
           for my $index4 ( @odd_list ) {
              $test_group[0] = $index0;
              $test_group[1] = $index1;
              $test_group[2] = $index2;
              $test_group[3] = $index3;
              $test_group[4] = $index4;

              my $sum = $test_group[0] + $test_group[1] + $test_group[2] + $test_group[3] + $test_group[4];
              last if($sum > 50); # From here all interations are greater than 50
              $eval_count++;

              if ($sum == 50) {
                 $sucess_count++;

                 my @ordered_group = sort { $a <=> $b } ( @test_group );
                 $unique_combinations{join(',', @ordered_group)} = 50;
              }
           }
        }
     }
  }

}

  1. Summary

print "Evaluated conditions : $eval_count\n"; print "Successful conditions: $sucess_count\n"; printf "Unique combinations  : %d\n", scalar keys %unique_combinations;

  1. Print unique succesful combinations

my $count = 1; for my $key (sort {$a cmp $b} keys %unique_combinations) {

  printf "%3d. SUM(%s)\t= %d\n", $count++, $key, $unique_combinations{$key};

} C++

  1. include <iostream>
  2. include <stdio.h>
  3. include <vector>
  4. include <algorithm>
  5. include <map>

using namespace std;

int main() {

  int eval_count = 1;
  int success_count = 1;
  std::map<string,int> unique_combinations;
  std::map<string,int>::iterator it;
  int odd_list[] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49 };
  for(int index0 = 1; index0 <= 26;) {
     int test_group[5] = {};
     for(int index1 = 1; index1 <= 26; index1++ ) {
        for(int index2 = 1; index2 <= 26; index2++ ) {
        for(int index3 = 1; index3 <= 26; index3++ ) {
           for(int index4 = 1; index4 <= 26; index4++ ) {
                 test_group[1] = odd_list[index1];
                 test_group[2] = odd_list[index2];
                 test_group[3] = odd_list[index3];
                 test_group[4] = odd_list[index4];
                 int sum = test_group[0] + test_group[1] + test_group[2] + test_group[3] + test_group[4];
                 eval_count++;
                 if(sum > 50) { break; }
                 if (sum == 50) {
                    success_count++;
                    string combination;
                    char buffer[50];
                    std::sort(test_group, test_group+5);
                    snprintf(buffer, 50, "%d,%d,%d,%d,%d", test_group[0], test_group[1], test_group[2], test_group[2], test_group[4]);
                    combination.append(buffer);
                    unique_combinations.insert(std::pair<string,int>(combination, 50));
                 }
              }
           }
        }
     }
  }
  cout << "Evaluated conditions : " << eval_count << endl;
  cout << "Successful conditions: " << success_count << endl;
  cout << "Unique combinations  : " <<  unique_combinations.size() << endl;
  for (std::map<string,int>::iterator it=unique_combinations.begin(); it!=unique_combinations.end(); ++it)
     cout << "SUM(" << it->first << ")\t= " << it->second << '\n';
  return 0;

}