Last active
January 10, 2019 23:26
-
-
Save sooop/4267ee4a48087f1801b88179a7a31336 to your computer and use it in GitHub Desktop.
Project Euler with Julia 001-010
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @elapsed sum(i for i=1:999 if i % 3 == 0 || i % 5 == 0) |> println | |
| ## FASTER SOLUTION | |
| @time (Set(3:3:999) ∪ Set(5:5:999)) |> sum |> println |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @time let (a, b) = (0, 1) | |
| s = 0 | |
| while b <= 400_0000 | |
| s += b % 2 == 0 ? b : 0 | |
| a, b = b, a + b | |
| end | |
| println(s) | |
| end | |
| ## Using Generator (FASTER!!!) | |
| let fib = Channel() do c | |
| a, b = 1, 1 | |
| while true | |
| put!(c, a) | |
| a, b = b, a+b | |
| end | |
| end | |
| s = 0 | |
| while true | |
| f = take!(fib) | |
| f > 400_0000 && break | |
| f % 2 == 0 && (s += f) | |
| end | |
| println(s) | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @time let b = 600851475143 | |
| a = 2 | |
| while b > a | |
| while b % a == 0 | |
| b = div(b, a) | |
| end | |
| a += 1 | |
| end | |
| println(b) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @time let ispalindrome(n) = "$n" == reverse("$n") | |
| (a*b for a=100:999, b=100:999 if ispalindrome(a*b)) |> maximum |> println | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @time reduce(lcm, 1:20) |> println |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @time begin | |
| abs(sum(1:100)^2 - sum(i^2 for i=1:100)) |> println | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function isprime(n) | |
| n < 2 && return false | |
| n in (2, 3) && return true | |
| (n % 2 == 0 || n % 3 == 0) && return false | |
| n < 8 && return true | |
| k = 5 | |
| l = Int(floor(√n + .5)) | |
| while k <= l | |
| (n % k == 0 || n % (k+2) == 0) && return false | |
| k += 6 | |
| end | |
| true | |
| end | |
| @time let p = Channel() do c | |
| put!(c, 2) | |
| a = 3 | |
| while true | |
| if isprime(a); put!(c, a); end | |
| a += 2 | |
| end | |
| end | |
| Iterators.take(p, 10001) |> collect |> last |> println | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @time let s = """73167176531330624919225119674426574742355349194934 | |
| 96983520312774506326239578318016984801869478851843 | |
| 85861560789112949495459501737958331952853208805511 | |
| 12540698747158523863050715693290963295227443043557 | |
| 66896648950445244523161731856403098711121722383113 | |
| 62229893423380308135336276614282806444486645238749 | |
| 30358907296290491560440772390713810515859307960866 | |
| 70172427121883998797908792274921901699720888093776 | |
| 65727333001053367881220235421809751254540594752243 | |
| 52584907711670556013604839586446706324415722155397 | |
| 53697817977846174064955149290862569321978468622482 | |
| 83972241375657056057490261407972968652414535100474 | |
| 82166370484403199890008895243450658541227588666881 | |
| 16427171479924442928230863465674813919123162824586 | |
| 17866458359124566529476545682848912883142607690042 | |
| 24219022671055626321111109370544217506941658960408 | |
| 07198403850962455444362981230987879927244284909188 | |
| 84580156166097919133875499200524063689912560717606 | |
| 05886116467109405077541002256983155200055935729725 | |
| 71636269561882670428252483600823257530420752963450""" | |
| xs = parse.(Int, (x for x in s if isdigit(x))) | |
| [prod(xs[i:i+4]) for i = 1:length(xs)-4] |> maximum |> println | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let xs = 3:999 | |
| result = 0 | |
| for a in xs | |
| for b in (a+1):999 | |
| c = 1000 - a - b | |
| result = (a^2+b^2==c^2) ? a*b*c : 0 | |
| result > 0 && (println(result);return) | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @time filter(isprime, 2:2e6) |> sum |> Int |> println | |
| # 속도를 개선한 버전 | |
| @time let n = 200_0000 | |
| s = trues(n) | |
| s[1] = false | |
| for i = (2:Int(floor(√n + 0.5))) | |
| if s[i] | |
| s[i+i:i:end] = zeros((n - i) ÷ i) | |
| end | |
| end | |
| sum([i for i = 1:n if s[i]]) |> println | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment