Skip to content

Commit 21bd65b

Browse files
committed
stashing changes
1 parent 11eaa43 commit 21bd65b

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

Primes.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Project euler problems in Haskell
2+
3+
module Primes where
4+
5+
primes :: [Int]
6+
primes = 2 : 3 : 5 : filter isPrime [7..]
7+
where isPrime :: Int -> Bool
8+
isPrime n = 0 == ( length ( filter (\c -> gcd n c > 1) (candidates n) ) )
9+
candidates :: Int -> [Int]
10+
candidates n = takeWhile (< ((sqrt . fromIntegral) n)) primes

prob.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Player A: 9 4-sided die
2+
# Player B: 6 6-sided die
3+
4+
# compute the probability of the sum of player A's die
5+
# being higher than the sum of player B's die
6+
7+
print (9**4) * (6**6)
8+
print (4**9) * (6**6)
9+
10+
def compA():
11+
A = [0 for k in range(36)]
12+
for n in xrange(len(A)):
13+
for a in xrange(1,5):
14+
for b in xrange(1,5):
15+
for c in xrange(1,5):
16+
for d in xrange(1,5):
17+
for e in xrange(1,5):
18+
for f in xrange(1,5):
19+
for g in xrange(1,5):
20+
for h in xrange(1,5):
21+
val = n + 1 - a - b - c - d - e - f - g - h
22+
if (val > 0) and (val < 5):
23+
A[n] += 1
24+
return A
25+
26+
A = compA()
27+
s = float(sum(A))
28+
for i, item in enumerate(A):
29+
A[i] = item / s
30+
31+
def compB():
32+
B = [0 for k in range(36)]
33+
for n in xrange(len(A)):
34+
for a in xrange(1,7):
35+
for b in xrange(1,7):
36+
for c in xrange(1,7):
37+
for d in xrange(1,7):
38+
for e in xrange(1,7):
39+
val = n + 1 - a - b - c - d - e
40+
if (val > 0) and (val < 7):
41+
B[n] += 1
42+
return B
43+
44+
B = compB()
45+
s = float(sum(B))
46+
for i, item in enumerate(B):
47+
B[i] = item / s
48+
49+
# compute answer
50+
51+
a = 0.0
52+
for i in range(36):
53+
for j in range(i):
54+
a += A[i] * B[j]
55+
print a
56+
57+
b = 0.0
58+
for i in range(36):
59+
for j in range(i):
60+
b += A[j] * B[i]
61+
print b
62+
63+
t = 0.0
64+
for i in range(36):
65+
t += A[i] * B[i]
66+
print t
67+
68+
print a + b + t

problem3.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <iostream>
2+
#include <stdio.h>
3+
#include <cmath>
4+
#include <vector>
5+
#include <map>
6+
7+
using namespace std;
8+
9+
typedef long long int lli;
10+
typedef long int li;
11+
typedef vector<bool> table;
12+
13+
lli compFactor(lli num);
14+
void buildTable(lli cap, table &lookup);
15+
lli firstFactor(lli num);
16+
17+
int main() {
18+
19+
lli bignum = 600851475143;
20+
21+
cout << "ANSWER: " << compFactor(bignum) << endl;
22+
return 0;
23+
}
24+
25+
lli compFactor(lli num) {
26+
lli prev, ans, current;
27+
current = num;
28+
prev = ans = 1;
29+
while(true) {
30+
prev = ans;
31+
ans = firstFactor(current);
32+
if(!ans) {
33+
break;
34+
}
35+
current = current / ans;
36+
// printf("num: %lld current: %lld ans: %lld prev: %lld\n", num, current, ans, prev);
37+
}
38+
39+
return current;
40+
}
41+
42+
43+
lli firstFactor(lli num) {
44+
table lookup;
45+
buildTable(sqrt(num),lookup);
46+
lli size = lookup.size();
47+
for(lli i = 3; i < size; i += 2) {
48+
if(lookup[i] && (num % i == 0)) {
49+
return i;
50+
}
51+
}
52+
return 0;
53+
}
54+
55+
void buildTable(lli cap, table &lookup) {
56+
lookup.resize(cap);
57+
lookup[0] = lookup[1] = false;
58+
for(lli i = 2; i < cap; i++) {
59+
lookup[i] = true;
60+
}
61+
for(lli i = 2; i < cap; i++) {
62+
if (lookup[i]) {
63+
for(lli j = 2 * i; j < cap; j += i) {
64+
lookup[j] = false;
65+
}
66+
}
67+
}
68+
return;
69+
}
70+
71+
bool isPrime(lli num, table lookup) {
72+
lli size = lookup.size();
73+
if (num < size) {
74+
return lookup[num];
75+
}
76+
77+
lli stop = sqrt(num);
78+
for(lli i = 3; i < stop; i += 2) {
79+
if (lookup[i] && (num % i == 0)) {
80+
return false;
81+
}
82+
}
83+
return true;
84+
}

0 commit comments

Comments
 (0)