From Wiki-UX.info

How to oddly kill 50 cows in 5 days
Jump to: navigation, search

How to oddly kill 50 cows in 5 days

Contents

Problem

You need to find how to kill 50 cows in just 5 days, knowing that each day you can only kill an odd number of cows.

Solution

There are 163 different way of killing 50 cows in 5 days, killing an odd number of cows each day... You only need to understand, that you can take a rest any other days... As a matter of fact, there is no valid combination is you try to work every day.

In case of doubt, remove the 0 from the @odd_list array.

Perl

use strict;
 
# Note that 0 is a valid combination, because you can decide to take a rest any other day
# 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 = ( 0, 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 = 0;
my $sucess_count = 0;
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;
               }
            }
         }
      }
   }
}
 
# Summary
print "Evaluated conditions : $eval_count\n";
print "Successful conditions: $sucess_count\n";
printf "Unique combinations  : %d\n", scalar keys %unique_combinations;
 
# 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++

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <map>
 
using namespace std;
 
int main() {
   int eval_count = 0;
   int success_count = 0;
   std::map<string,int> unique_combinations;
   std::map<string,int>::iterator it;
 
   int odd_list[] = { 0, 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 = 0; index0 <= 26; index0++ ) {
      int test_group[5] = {};
 
      for(int index1 = 0; index1 <= 26; index1++ ) {
         for(int index2 = 0; index2 <= 26; index2++ ) {
         for(int index3 = 0; index3 <= 26; index3++ ) {
            for(int index4 = 0; index4 <= 26; index4++ ) {
                  test_group[0] = odd_list[index0];
                  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;
}

Valid odd cow killing combinations

Evaluated conditions: 197856
Sucessful conditions: 13250
Unique combinations : 163
  1. SUM(0,0,0,1,49)    = 50
  2. SUM(0,0,0,11,39)   = 50
  3. SUM(0,0,0,13,37)   = 50
  4. SUM(0,0,0,15,35)   = 50
  5. SUM(0,0,0,17,33)   = 50
  6. SUM(0,0,0,19,31)   = 50
  7. SUM(0,0,0,21,29)   = 50
  8. SUM(0,0,0,23,27)   = 50
  9. SUM(0,0,0,25,25)   = 50
 10. SUM(0,0,0,3,47)    = 50
 11. SUM(0,0,0,5,45)    = 50
 12. SUM(0,0,0,7,43)    = 50
 13. SUM(0,0,0,9,41)    = 50
 14. SUM(0,1,1,1,47)    = 50
 15. SUM(0,1,1,11,37)   = 50
 16. SUM(0,1,1,13,35)   = 50
 17. SUM(0,1,1,15,33)   = 50
 18. SUM(0,1,1,17,31)   = 50
 19. SUM(0,1,1,19,29)   = 50
 20. SUM(0,1,1,21,27)   = 50
 21. SUM(0,1,1,23,25)   = 50
 22. SUM(0,1,1,3,45)    = 50
 23. SUM(0,1,1,5,43)    = 50
 24. SUM(0,1,1,7,41)    = 50
 25. SUM(0,1,1,9,39)    = 50
 26. SUM(0,1,11,11,27)  = 50
 27. SUM(0,1,11,13,25)  = 50
 28. SUM(0,1,11,15,23)  = 50
 29. SUM(0,1,11,17,21)  = 50
 30. SUM(0,1,11,19,19)  = 50
 31. SUM(0,1,13,13,23)  = 50
 32. SUM(0,1,13,15,21)  = 50
 33. SUM(0,1,13,17,19)  = 50
 34. SUM(0,1,15,15,19)  = 50
 35. SUM(0,1,15,17,17)  = 50
 36. SUM(0,1,3,11,35)   = 50
 37. SUM(0,1,3,13,33)   = 50
 38. SUM(0,1,3,15,31)   = 50
 39. SUM(0,1,3,17,29)   = 50
 40. SUM(0,1,3,19,27)   = 50
 41. SUM(0,1,3,21,25)   = 50
 42. SUM(0,1,3,23,23)   = 50
 43. SUM(0,1,3,3,43)    = 50
 44. SUM(0,1,3,5,41)    = 50
 45. SUM(0,1,3,7,39)    = 50
 46. SUM(0,1,3,9,37)    = 50
 47. SUM(0,1,5,11,33)   = 50
 48. SUM(0,1,5,13,31)   = 50
 49. SUM(0,1,5,15,29)   = 50
 50. SUM(0,1,5,17,27)   = 50
 51. SUM(0,1,5,19,25)   = 50
 52. SUM(0,1,5,21,23)   = 50
 53. SUM(0,1,5,5,39)    = 50
 54. SUM(0,1,5,7,37)    = 50
 55. SUM(0,1,5,9,35)    = 50
 56. SUM(0,1,7,11,31)   = 50
 57. SUM(0,1,7,13,29)   = 50
 58. SUM(0,1,7,15,27)   = 50
 59. SUM(0,1,7,17,25)   = 50
 60. SUM(0,1,7,19,23)   = 50
 61. SUM(0,1,7,21,21)   = 50
 62. SUM(0,1,7,7,35)    = 50
 63. SUM(0,1,7,9,33)    = 50
 64. SUM(0,1,9,11,29)   = 50
 65. SUM(0,1,9,13,27)   = 50
 66. SUM(0,1,9,15,25)   = 50
 67. SUM(0,1,9,17,23)   = 50
 68. SUM(0,1,9,19,21)   = 50
 69. SUM(0,1,9,9,31)    = 50
 70. SUM(0,11,11,11,17) = 50
 71. SUM(0,11,11,13,15) = 50
 72. SUM(0,11,13,13,13) = 50
 73. SUM(0,3,11,11,25)  = 50
 74. SUM(0,3,11,13,23)  = 50
 75. SUM(0,3,11,15,21)  = 50
 76. SUM(0,3,11,17,19)  = 50
 77. SUM(0,3,13,13,21)  = 50
 78. SUM(0,3,13,15,19)  = 50
 79. SUM(0,3,13,17,17)  = 50
 80. SUM(0,3,15,15,17)  = 50
 81. SUM(0,3,3,11,33)   = 50
 82. SUM(0,3,3,13,31)   = 50
 83. SUM(0,3,3,15,29)   = 50
 84. SUM(0,3,3,17,27)   = 50
 85. SUM(0,3,3,19,25)   = 50
 86. SUM(0,3,3,21,23)   = 50
 87. SUM(0,3,3,3,41)    = 50
 88. SUM(0,3,3,5,39)    = 50
 89. SUM(0,3,3,7,37)    = 50
 90. SUM(0,3,3,9,35)    = 50
 91. SUM(0,3,5,11,31)   = 50
 92. SUM(0,3,5,13,29)   = 50
 93. SUM(0,3,5,15,27)   = 50
 94. SUM(0,3,5,17,25)   = 50
 95. SUM(0,3,5,19,23)   = 50
 96. SUM(0,3,5,21,21)   = 50
 97. SUM(0,3,5,5,37)    = 50
 98. SUM(0,3,5,7,35)    = 50
 99. SUM(0,3,5,9,33)    = 50
100. SUM(0,3,7,11,29)   = 50
101. SUM(0,3,7,13,27)   = 50
102. SUM(0,3,7,15,25)   = 50
103. SUM(0,3,7,17,23)   = 50
104. SUM(0,3,7,19,21)   = 50
105. SUM(0,3,7,7,33)    = 50
106. SUM(0,3,7,9,31)    = 50
107. SUM(0,3,9,11,27)   = 50
108. SUM(0,3,9,13,25)   = 50
109. SUM(0,3,9,15,23)   = 50
110. SUM(0,3,9,17,21)   = 50
111. SUM(0,3,9,19,19)   = 50
112. SUM(0,3,9,9,29)    = 50
113. SUM(0,5,11,11,23)  = 50
114. SUM(0,5,11,13,21)  = 50
115. SUM(0,5,11,15,19)  = 50
116. SUM(0,5,11,17,17)  = 50
117. SUM(0,5,13,13,19)  = 50
118. SUM(0,5,13,15,17)  = 50
119. SUM(0,5,15,15,15)  = 50
120. SUM(0,5,5,11,29)   = 50
121. SUM(0,5,5,13,27)   = 50
122. SUM(0,5,5,15,25)   = 50
123. SUM(0,5,5,17,23)   = 50
124. SUM(0,5,5,19,21)   = 50
125. SUM(0,5,5,5,35)    = 50
126. SUM(0,5,5,7,33)    = 50
127. SUM(0,5,5,9,31)    = 50
128. SUM(0,5,7,11,27)   = 50
129. SUM(0,5,7,13,25)   = 50
130. SUM(0,5,7,15,23)   = 50
131. SUM(0,5,7,17,21)   = 50
132. SUM(0,5,7,19,19)   = 50
133. SUM(0,5,7,7,31)    = 50
134. SUM(0,5,7,9,29)    = 50
135. SUM(0,5,9,11,25)   = 50
136. SUM(0,5,9,13,23)   = 50
137. SUM(0,5,9,15,21)   = 50
138. SUM(0,5,9,17,19)   = 50
139. SUM(0,5,9,9,27)    = 50
140. SUM(0,7,11,11,21)  = 50
141. SUM(0,7,11,13,19)  = 50
142. SUM(0,7,11,15,17)  = 50
143. SUM(0,7,13,13,17)  = 50
144. SUM(0,7,13,15,15)  = 50
145. SUM(0,7,7,11,25)   = 50
146. SUM(0,7,7,13,23)   = 50
147. SUM(0,7,7,15,21)   = 50
148. SUM(0,7,7,17,19)   = 50
149. SUM(0,7,7,7,29)    = 50
150. SUM(0,7,7,9,27)    = 50
151. SUM(0,7,9,11,23)   = 50
152. SUM(0,7,9,13,21)   = 50
153. SUM(0,7,9,15,19)   = 50
154. SUM(0,7,9,17,17)   = 50
155. SUM(0,7,9,9,25)    = 50
156. SUM(0,9,11,11,19)  = 50
157. SUM(0,9,11,13,17)  = 50
158. SUM(0,9,11,15,15)  = 50
159. SUM(0,9,13,13,15)  = 50
160. SUM(0,9,9,11,21)   = 50
161. SUM(0,9,9,13,19)   = 50
162. SUM(0,9,9,15,17)   = 50
163. SUM(0,9,9,9,23)    = 50

Authors

This page was last modified on 22 April 2014, at 22:47. This page has been accessed 2,998 times.