Math 331: Introduction to Coding

Numbers 1 through 9 to get three fractions adding to 1

list = {};
For[i = 1, i ≤ 9, i++, list = AppendTo[list, i]];
permlist = Permutations[list];
For[n = 1, n ≤ Length[permlist], n++,
{
temp = permlist[[n]];
x = Sum[temp[[1+3*i]] / (10 temp[[2+3*i]] + temp[[3+3*i]]) , {i,0,2}];
If[x == 1, Print[temp]];
}]; (* end of the n for loop *)

Ruth-Aaron Numbers

(* program to find sum of prime factors *)
primefactorsum[x_] := Module[{},
temp = FactorInteger[x];
Return[Sum[temp[[n,1]] temp[[n,2]],{n,1,Length[temp]}]];
] (* end of module *)
ruthaaronsearch[numdo_] := Module[{},
lower = primefactorsum[2];
success = 0;
For[m = 3, m ≤ numdo + 2, m++,
{
current = primefactorsum[m];
If[current == lower, 
{
Print[m-1, " ", m, " ", current];
success = success + 1;
}]; (* end of if loop *)
lower = current;
}]; (* end of m for loop *)
Print["Found ", success, " in first ", numdo, "."];
] (* end of module *)

ruthaaronsearch[10000]

Getting at least 2 Kings and at least 2 Queens with 5 cards

twokingtwoqueen[numdo_] := Module[{},
(* initialize deck *)
deck = {};
For[c = 1, c ≤4, c++, deck = AppendTo[deck,1]];
For[c = 5, c ≤8, c++, deck = AppendTo[deck,10]];
For[c = 9, c ≤52, c++, deck = AppendTo[deck,0]];
success = 0;
For[n = 1, n ≤ numdo, n++,
{
If[Mod[n,numdo/10] == 0, Print["Have done ", 100.0 n/numdo, "%."]];
hand = RandomSample[deck,5]; (* chooses 5 card from deck randomly *)
value = Sum[hand[[c]], {c,1,5}];
If[value ≥ 20, 
If[Mod[value,10] ≥ 2, success = success + 1]];
}]; (* end of n for loop *)
Print["Success rate is ", 100.0 * success / numdo, "%."];
Print["Answer is ", 100.0 (2 Binomial[4,2] Binomial[4,3]+Binomial[4,2]^2 Binomial[44,1])/Binomial[52,5], "%."];
] (* end of module *)

Timing[twokingtwoqueen[1000000]]

Timing[twokingtwoqueen[10000000]]

Timing[twokingtwoqueen[100000000]]