Skip to content

Commit 19ebaed

Browse files
committed
project euler
1 parent f50031e commit 19ebaed

File tree

5 files changed

+166
-3
lines changed

5 files changed

+166
-3
lines changed

ProjectEuler/gen_prime.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
open Hashtbl
22

3-
let get_seq ?(x = 1) ?(inc = 1) n =
3+
let range ?(x = 1) ?(inc = 1) n =
44
let rec aux acc x =
55
if x > n then acc else aux (x :: acc) (x + inc)
66
in
77
List.rev (aux [] x)
88

9-
109
let gen_prime n =
1110
let update x step lst =
1211
let rec aux acc cur y =
@@ -28,9 +27,14 @@ let gen_prime n =
2827
else aux (x :: acc) (update (x*x) x xs)
2928
end
3029
in
31-
aux [] (List.tl (get_seq n))
30+
aux [] (List.tl (range n))
3231

3332

3433
let pm_list = gen_prime 100000
3534
let pm_tb = Hashtbl.create (List.length pm_list)
3635
let () = List.iter (fun x -> Hashtbl.add pm_tb x true) pm_list
36+
37+
let ( |> ) v f = f v
38+
let ( <| ) f v = f v
39+
let foi = float_of_int
40+
let iof = int_of_float

ProjectEuler/p053.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
ans = 0;
2+
c = Array.new(101) {Array.new(101)}
3+
c[0][0] = 1;
4+
(1..100).each{|i|
5+
c[i][0] = 1; c[i][i] = 1;
6+
(1..i-1).each {|j|
7+
if c[i-1][j-1] == -1 || c[i-1][j] == -1 || c[i-1][j-1] + c[i-1][j] > 1000000
8+
c[i][j] = -1;
9+
ans += 1;
10+
else
11+
c[i][j] = c[i-1][j-1] + c[i-1][j]
12+
end
13+
}
14+
}
15+
p ans
16+

ProjectEuler/p068.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <cstdio>
2+
#include <cstring>
3+
#include <algorithm>
4+
#include <string>
5+
#include <iostream>
6+
#include <cstdlib>
7+
#include <sstream>
8+
9+
using namespace std;
10+
11+
int a[11];
12+
bool flag[11];
13+
string ans;
14+
int idx[15] = {1,6,7,2,7,8,3,8,9,4,9,10,5,10,6};
15+
int b[7] = {0, 6, 7, 1, 2, 3, 4};
16+
17+
string trans () {
18+
stringstream ret;
19+
for (int i = 0; i < 15; ++i)
20+
ret << a[idx[i]];
21+
return ret.str();
22+
}
23+
24+
bool chk () {
25+
int i, j, k;
26+
k = a[1] + a[6] + a[7];
27+
bool flag1[11];
28+
memcpy(flag1, flag, sizeof (flag));
29+
30+
j = a[8] = k - a[2] - a[7];
31+
if(j < 1 || j > 10 || flag1[j]) return false; else flag1[j] = true;
32+
33+
j = a[9] = k - a[3] - a[8];
34+
if(j < 1 || j > 10 || flag1[j]) return false; else flag1[j] = true;
35+
36+
j = a[10] = k - a[4] - a[9];
37+
if(j < 1 || j > 10 || flag1[j]) return false; else flag1[j] = true;
38+
39+
j = a[5] = k - a[6] - a[10];
40+
if(j < 1 || j > 10 || flag1[j] || j <= a[1]) return false; else flag1[j] = true;
41+
42+
return true;
43+
}
44+
45+
void DFS (int n, int x) {
46+
int i, j, k;
47+
if (n == 7) {
48+
if (chk ())
49+
ans = max (ans, trans());
50+
return;
51+
}
52+
if (n > 3 && n <= 6 && x <= a[1]) return;
53+
flag[a[b[n]] = x] = true;
54+
55+
for (i = 1; i <= 10; ++i) {
56+
if (flag[i]) continue;
57+
DFS (n + 1, i);
58+
}
59+
flag[x] = false;
60+
}
61+
62+
int main () {
63+
ans = "000000";
64+
for (int i = 1; i <= 10; ++i) {
65+
memset (flag, false, sizeof(flag));
66+
DFS (1, i);
67+
}
68+
cout << ans << endl;
69+
}

ProjectEuler/p085.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
let range ?(x = 1) ?(inc = 1) n =
2+
let rec aux acc x =
3+
if x > n then acc else aux (x :: acc) (x + inc)
4+
in
5+
List.rev (aux [] x)
6+
7+
8+
let g acc x =
9+
List.fold_left (fun acc y ->
10+
let cnt = x * (x - 1) * y * (y - 1) / 4 in
11+
let diff = abs (cnt - 2000000) in
12+
if diff < (List.hd acc) then
13+
[diff; x; y]
14+
else
15+
acc)
16+
acc (range ~x:x 100)
17+
18+
let ret = List.fold_left g [100000; 0; 0] (range ~x:2 100) in
19+
(List.hd (List.rev ret) - 1) * (List.hd (List.tl ret) - 1)
20+

ProjectEuler/p087.ml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
open Hashtbl
2+
3+
let get_seq ?(x = 1) ?(inc = 1) n =
4+
let rec aux acc x =
5+
if x > n then acc else aux (x :: acc) (x + inc)
6+
in
7+
List.rev (aux [] x)
8+
9+
10+
let gen_prime n =
11+
let update x step lst =
12+
let rec aux acc cur y =
13+
match y with
14+
| [] -> List.rev acc
15+
| h :: hs -> begin
16+
if cur < h then aux acc (cur + step) y
17+
else if h < cur then aux (h :: acc) cur hs else aux acc (cur + step) hs
18+
end
19+
in
20+
aux [] x lst
21+
in
22+
23+
let rec aux acc lst =
24+
match lst with
25+
| [] -> List.rev acc
26+
| x :: xs -> begin
27+
if x * x > n then aux (x :: acc) xs
28+
else aux (x :: acc) (update (x*x) x xs)
29+
end
30+
in
31+
aux [] (List.tl (get_seq n))
32+
33+
34+
let pm_list = gen_prime 10000
35+
let tb = Hashtbl.create 5000011
36+
let arr_2 = Array.of_list (List.filter (fun x -> x * x <= 50000000) pm_list)
37+
let arr_3 = Array.of_list (List.filter (fun x -> x * x * x <= 50000000) pm_list)
38+
let arr_4 = Array.of_list (List.filter (fun x -> x * x * x * x <= 50000000) pm_list)
39+
let n2 = Array.length arr_2
40+
let n3 = Array.length arr_3
41+
let n4 = Array.length arr_4
42+
43+
44+
for i = 0 to n2-1 do
45+
for j = 0 to n3-1 do
46+
for k = 0 to n4-1 do
47+
Hashtbl.add tb (arr.(i) * arr.(i) + arr.(j) * arr.(j) * arr.(j)
48+
+ arr.(k) * arr.(k) * arr.(k) * arr.(k)) true
49+
done;
50+
done;
51+
done
52+
53+
let ans = List.fold_left (fun acc x -> if Hashtbl.mem tb x then 1 + acc else acc)
54+
0 (get_seq 50000000)

0 commit comments

Comments
 (0)