Skip to content

Commit 37c6fdb

Browse files
committed
All latest
1 parent e6e6a94 commit 37c6fdb

File tree

728 files changed

+49178
-91
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

728 files changed

+49178
-91
lines changed
539 Bytes
Binary file not shown.

BroadCast/.idea/codeStyles/Project.xml

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BroadCast/.idea/misc.xml

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Euler/Answers.txt

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
Project Euler numerical answers
2+
Computed by Project Nayuki
3+
4+
https://www.nayuki.io/page/project-euler-solutions
5+
https://github.com/nayuki/Project-Euler-solutions
6+
7+
8+
Problem 001: 233168
9+
Problem 002: 4613732
10+
Problem 003: 6857
11+
Problem 004: 906609
12+
Problem 005: 232792560
13+
Problem 006: 25164150
14+
Problem 007: 104743
15+
Problem 008: 23514624000
16+
Problem 009: 31875000
17+
Problem 010: 142913828922
18+
Problem 011: 70600674
19+
Problem 012: 76576500
20+
Problem 013: 5537376230
21+
Problem 014: 837799
22+
Problem 015: 137846528820
23+
Problem 016: 1366
24+
Problem 017: 21124
25+
Problem 018: 1074
26+
Problem 019: 171
27+
Problem 020: 648
28+
Problem 021: 31626
29+
Problem 022: 871198282
30+
Problem 023: 4179871
31+
Problem 024: 2783915460
32+
Problem 025: 4782
33+
Problem 026: 983
34+
Problem 027: -59231
35+
Problem 028: 669171001
36+
Problem 029: 9183
37+
Problem 030: 443839
38+
Problem 031: 73682
39+
Problem 032: 45228
40+
Problem 033: 100
41+
Problem 034: 40730
42+
Problem 035: 55
43+
Problem 036: 872187
44+
Problem 037: 748317
45+
Problem 038: 932718654
46+
Problem 039: 840
47+
Problem 040: 210
48+
Problem 041: 7652413
49+
Problem 042: 162
50+
Problem 043: 16695334890
51+
Problem 044: 5482660
52+
Problem 045: 1533776805
53+
Problem 046: 5777
54+
Problem 047: 134043
55+
Problem 048: 9110846700
56+
Problem 049: 296962999629
57+
Problem 050: 997651
58+
Problem 051: 121313
59+
Problem 052: 142857
60+
Problem 053: 4075
61+
Problem 054: 376
62+
Problem 055: 249
63+
Problem 056: 972
64+
Problem 057: 153
65+
Problem 058: 26241
66+
Problem 059: 107359
67+
Problem 060: 26033
68+
Problem 061: 28684
69+
Problem 062: 127035954683
70+
Problem 063: 49
71+
Problem 064: 1322
72+
Problem 065: 272
73+
Problem 066: 661
74+
Problem 067: 7273
75+
Problem 068: 6531031914842725
76+
Problem 069: 510510
77+
Problem 070: 8319823
78+
Problem 071: 428570
79+
Problem 072: 303963552391
80+
Problem 073: 7295372
81+
Problem 074: 402
82+
Problem 075: 161667
83+
Problem 076: 190569291
84+
Problem 077: 71
85+
Problem 078: 55374
86+
Problem 079: 73162890
87+
Problem 080: 40886
88+
Problem 081: 427337
89+
Problem 082: 260324
90+
Problem 083: 425185
91+
Problem 084: 101524
92+
Problem 085: 2772
93+
Problem 086: 1818
94+
Problem 087: 1097343
95+
Problem 088: 7587457
96+
Problem 089: 743
97+
Problem 090: 1217
98+
Problem 091: 14234
99+
Problem 092: 8581146
100+
Problem 093: 1258
101+
Problem 094: 518408346
102+
Problem 095: 14316
103+
Problem 096: 24702
104+
Problem 097: 8739992577
105+
Problem 098: 18769
106+
Problem 099: 709
107+
Problem 100: 756872327473
108+
Problem 101: 37076114526
109+
Problem 102: 228
110+
Problem 103: 20313839404245
111+
Problem 104: 329468
112+
Problem 105: 73702
113+
Problem 106: 21384
114+
Problem 107: 259679
115+
Problem 108: 180180
116+
Problem 109: 38182
117+
Problem 111: 612407567715
118+
Problem 112: 1587000
119+
Problem 113: 51161058134250
120+
Problem 114: 16475640049
121+
Problem 115: 168
122+
Problem 116: 20492570929
123+
Problem 117: 100808458960497
124+
Problem 118: 44680
125+
Problem 119: 248155780267521
126+
Problem 120: 333082500
127+
Problem 121: 2269
128+
Problem 122: 1582
129+
Problem 123: 21035
130+
Problem 124: 21417
131+
Problem 125: 2906969179
132+
Problem 127: 18407904
133+
Problem 128: 14516824220
134+
Problem 129: 1000023
135+
Problem 130: 149253
136+
Problem 132: 843296
137+
Problem 133: 453647705
138+
Problem 134: 18613426663617118
139+
Problem 135: 4989
140+
Problem 139: 10057761
141+
Problem 142: 1006193
142+
Problem 145: 608720
143+
Problem 146: 676333270
144+
Problem 149: 52852124
145+
Problem 150: -271248680
146+
Problem 151: 0.464399
147+
Problem 155: 3857447
148+
Problem 160: 16576
149+
Problem 162: 3D58725572C62302
150+
Problem 164: 378158756814587
151+
Problem 166: 7130034
152+
Problem 169: 178653872807
153+
Problem 171: 142989277
154+
Problem 172: 227485267000992000
155+
Problem 173: 1572729
156+
Problem 174: 209566
157+
Problem 178: 126461847755
158+
Problem 179: 986262
159+
Problem 182: 399788195976
160+
Problem 186: 2325629
161+
Problem 187: 17427258
162+
Problem 188: 95962097
163+
Problem 191: 1918080160
164+
Problem 197: 1.710637717
165+
Problem 203: 34029210557338
166+
Problem 204: 2944730
167+
Problem 205: 0.5731441
168+
Problem 206: 1389019170
169+
Problem 208: 331951449665644800
170+
Problem 211: 1922364685
171+
Problem 214: 1677366278943
172+
Problem 215: 806844323190414
173+
Problem 216: 5437849
174+
Problem 218: 0
175+
Problem 222: 1590933
176+
Problem 225: 2009
177+
Problem 231: 7526965179680
178+
Problem 243: 892371480
179+
Problem 249: 9275262564250418
180+
Problem 250: 1425480602091519
181+
Problem 265: 209110240768
182+
Problem 267: 0.999992836187
183+
Problem 271: 4617456485273129588
184+
Problem 280: 430.088247
185+
Problem 287: 313135496
186+
Problem 301: 2178309
187+
Problem 303: 1111981904675169
188+
Problem 304: 283988410192
189+
Problem 315: 13625242
190+
Problem 323: 6.3551758451
191+
Problem 329: 199740353/29386561536000
192+
Problem 345: 13938
193+
Problem 346: 336108797689259276
194+
Problem 347: 11109800204052
195+
Problem 348: 1004195061
196+
Problem 357: 1739023853137
197+
Problem 381: 139602943319822
198+
Problem 387: 696067597313468
199+
Problem 401: 281632621
200+
Problem 407: 39782849136421
201+
Problem 417: 446572970925740
202+
Problem 425: 46479497324
203+
Problem 429: 98792821
204+
Problem 431: 23.386029052
205+
Problem 433: 326624372659664
206+
Problem 451: 153651073760956
207+
Problem 493: 6.818741802
208+
Problem 500: 35407281
209+
Problem 518: 100315739184392
210+
Problem 549: 476001479068717
211+
Problem 587: 2240

Euler/Readme.markdown

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Project Euler solutions
2+
=======================
3+
4+
A collection of Nayuki's program code to solve over 200 Project Euler math problems.
5+
6+
Every solved problem has a program written in Java and usually Python. Some solutions also have Mathematica and Haskell programs. Some solution programs include a detailed mathematical explanation/proof in the comments to justify the code's logic.
7+
8+
All problems from #1 to #100 have a Java and Python program, and problems #1 to #50 have a Mathematica program. This package contains at least 200 solutions in Java, at least 195 in Python, at least 120 in Mathematica, and at least 85 in Haskell.
9+
10+
Java solutions require JDK 7+. Python solutions are tested to work on CPython 2.7.10 and 3.4.3. Mathematica solutions are tested to work on Mathematica 5.1.
11+
12+
Home page with background info, table of solutions, benchmark timings, and more: [https://www.nayuki.io/page/project-euler-solutions](https://www.nayuki.io/page/project-euler-solutions)
13+
14+
----
15+
16+
Copyright © 2018 Project Nayuki. All rights reserved. No warranty.
17+
18+
This code is provided for reference only. You may republish any of this code verbatim with author and URL info intact.
19+
20+
You need written permission from the author to make modifications to the code, include parts into your own work, etc.

Euler/haskell/EulerLib.hs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{-
2+
- Shared code for solutions to Project Euler problems
3+
- Copyright (c) Project Nayuki. All rights reserved.
4+
-
5+
- https://www.nayuki.io/page/project-euler-solutions
6+
- https://github.com/nayuki/Project-Euler-solutions
7+
-}
8+
9+
module EulerLib
10+
(boolToInt, count, factorial, binomial, digitSum, sqrt, isPrime)
11+
where
12+
13+
import Prelude hiding (sqrt)
14+
import Data.Bits
15+
16+
17+
boolToInt :: Integral a => Bool -> a
18+
boolToInt False = 0
19+
boolToInt True = 1
20+
21+
22+
-- Counts the number of elements in the list that satisfy the predicate.
23+
count :: (a -> Bool) -> [a] -> Int
24+
count pred list = length (filter pred list)
25+
-- Or shorter: count = length . filter
26+
27+
28+
factorial :: Integral a => a -> Integer
29+
factorial n = product [1 .. (toInteger n)]
30+
31+
32+
binomial :: Integral a => a -> a -> Integer
33+
binomial n k = div (product [(toInteger (n - k + 1)) .. (toInteger n)]) (factorial k)
34+
35+
36+
digitSum :: Integral a => a -> a
37+
digitSum 0 = 0
38+
digitSum n = (mod n 10) + (digitSum (div n 10))
39+
40+
41+
-- sqrt n = floor(sqrt(n)).
42+
-- Implemented entirely in integer arithmetic; guaranteed no rounding error.
43+
sqrt :: (Integral a, Data.Bits.Bits a) => a -> a
44+
sqrt n = sqrtAlpha 1 where
45+
sqrtAlpha i
46+
| i * i > n = sqrtBeta (Data.Bits.shiftR i 1) 0
47+
| otherwise = sqrtAlpha (Data.Bits.shiftL i 1)
48+
sqrtBeta 0 acc = acc
49+
sqrtBeta i acc = sqrtBeta (div i 2) (if (i + acc)^2 <= n then i + acc else acc)
50+
51+
52+
isPrime :: (Integral a, Data.Bits.Bits a) => a -> Bool
53+
isPrime n = n > 1 && null [() | k <- [2 .. (sqrt n)], mod n k == 0]

Euler/haskell/p001.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{-
2+
- Solution to Project Euler problem 1
3+
- Copyright (c) Project Nayuki. All rights reserved.
4+
-
5+
- https://www.nayuki.io/page/project-euler-solutions
6+
- https://github.com/nayuki/Project-Euler-solutions
7+
-}
8+
9+
10+
-- Computers are fast, so we can implement this solution directly without any clever math.
11+
main = putStrLn (show ans)
12+
ans = sum [x | x <- [0..999], (mod x 3) == 0 || (mod x 5) == 0]

Euler/haskell/p002.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{-
2+
- Solution to Project Euler problem 2
3+
- Copyright (c) Project Nayuki. All rights reserved.
4+
-
5+
- https://www.nayuki.io/page/project-euler-solutions
6+
- https://github.com/nayuki/Project-Euler-solutions
7+
-}
8+
9+
10+
-- Computers are fast, so we can implement this solution directly without any clever math.
11+
main = putStrLn (show ans)
12+
ans = sum $ filter even $ takeWhile (<= 4000000) fibonacci
13+
14+
-- A lazy infinite sequence of numbers
15+
fibonacci = 1 : 2 : (zipWith (+) fibonacci (tail fibonacci))

Euler/haskell/p003.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{-
2+
- Solution to Project Euler problem 3
3+
- Copyright (c) Project Nayuki. All rights reserved.
4+
-
5+
- https://www.nayuki.io/page/project-euler-solutions
6+
- https://github.com/nayuki/Project-Euler-solutions
7+
-}
8+
9+
10+
-- By the fundamental theorem of arithmetic, every integer n > 1 has a unique factorization as a product of prime numbers.
11+
-- In other words, the theorem says that n = p_0 * p_1 * ... * p_{m-1}, where each p_i > 1 is prime but not necessarily unique.
12+
-- Now if we take the number n and repeatedly divide out its smallest factor (which must also be prime), then the last
13+
-- factor that we divide out must be the largest prime factor of n. For reference, 600851475143 = 71 * 839 * 1471 * 6857.
14+
main = putStrLn (show ans)
15+
ans = largestPrimeFactor (600851475143 :: Integer)
16+
17+
largestPrimeFactor n =
18+
let
19+
p = smallestPrimeFactor n
20+
in
21+
if p == n then p
22+
else largestPrimeFactor (div n p)
23+
24+
smallestPrimeFactor n = head [k | k <- [2..n], mod n k == 0]

0 commit comments

Comments
 (0)