Quantcast
Channel: RobPaulson.com
Viewing all articles
Browse latest Browse all 10

A Farmer With Four Stones.....

$
0
0

A farmer had a stone that he used to measure grain on his scale. One day his neighbor borrowed the stone; and when he returned; it was broken into four pieces. The neighbor was very apologetic; but the farmer thanked the neighbor for doing him a big favor. The farmer said that now he can measure his grain in one pound increments starting at one pound all the way to forty pounds (1; 2; 3; 17; 29; 37; etc.) using these four stones. How much do the four stones weigh?

Solution with LINQ...

 var a = ((from myA in Enumerable.Range(1, 40)
		 from myB in Enumerable.Range(1, 40)
		 from myC in Enumerable.Range(1, 40)
		 from myD in Enumerable.Range(1, 40)
			 where myA + myB + myC + myD == 40&& myA <= myB&& myB <= myC&& myC <= myD
		 select new[] {myA, myB, myC, myD}).Where(
					   myNum => Enumerable.Range(1, 40).All(
								target => ((from myn1 in new[] { 0, 1, -1 }
											from myn2 in new[] { 0, 1, -1 }
											from myn3 in new[] { 0, 1, -1 }
											from myn4 in new[] { 0, 1, -1 }
											select myn1*myNum[0]
												   + myn2*myNum[1]
												   + myn3*myNum[2]
												   + myn4*myNum[3]).
											  Any
											  (x => x == target))))).ToList();

Viewing all articles
Browse latest Browse all 10

Trending Articles