In [34]:
    
using TimeIt
using BenchmarkTools
    
    
In [11]:
    
@time sum([x for x in 1:999 if ((x%3 ==0) || (x%5 == 0))])
    
    
    Out[11]:
In [12]:
    
@time sum(x for x in 1:999 if ((x%3 == 0) || (x%5 ==0)))
    
    
    Out[12]:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
In [1]:
    
immutable Fibonacci
end
    
In [2]:
    
Base.start(f::Fibonacci) = (0,1)
Base.next(f::Fibonacci, state) = (state[2], (state[2], state[1] + state[2]))
Base.done(f::Fibonacci, state) = false 
Base.iteratorsize(f::Fibonacci) = Base.IsInfinite()
    
In [13]:
    
sum(collect(filter(x -> iseven(x) && x < Int(4e6), take(Fibonacci(), 50))))
    
    Out[13]:
In [35]:
    
using Lazy
    
In [18]:
    
@timeit sum(filter(iseven, takewhile(x->x<4e6,Fibonacci())))
    
    
    Out[18]:
In [49]:
    
#sieve of Erasthosenes
function PrimeSieve(N::Int64)
    isprime = trues(N)
    for i = 2:Int64(trunc(sqrt(N)))
        if isprime[i]
            for j in (i*i):i:N
                isprime[j] = false
            end
        end
    end
    return filter(x -> isprime[x], 2:N)
end
    
    Out[49]:
In [20]:
    
function LargestPrimeFactor(N::Int64)
    for x in reverse(Primes(10000))
        if N % x == 0
            return x
        end
    end
    return N
end
    
    
    Out[20]:
    
In [23]:
    
@timeit LargestPrimeFactor(600851475143)
    
    
    Out[23]:
In [24]:
    
@timeit maximum(filter(x -> reverse("$x") == "$x", [x*y for x in 100:999 for y in x:999]))
    
    
    Out[24]:
In [27]:
    
@timeit maximum(filter(x -> reverse("$x") == "$x", (x*y for x in 999:-1:100 for y in 999:-1:x)))
    
    
    Out[27]:
In [35]:
    
@timeit maximum(unique([x*y for x in 100:999 for y in x:999 if reverse("$(x*y)") == "$(x*y)"]))
    
    
    Out[35]:
In [2]:
    
#Euclid's algorithm for GCD
function GCD(a::Int64, b::Int64)
    if b == 0
        return a
    else
        return GCD(b, a % b)
    end
end 
LCM(a::Int64, b::Int64) = div(abs(a), GCD(a,b)) * abs(b)
    
    Out[2]:
In [64]:
    
@timeit reduce(LCM, 1, 1:20)
    
    
    Out[64]:
In [71]:
    
sum(1:100)^2 - sum(map(x -> x^2, 1:100))
    
    Out[71]:
In [1]:
    
immutable PrimeIt
end
    
In [30]:
    
Base.start(::PrimeIt) = (2, Dict{Int64, Int64}())
function Base.next(::PrimeIt, state)
    q = state[1]
    D = state[2]
    while true
        if q not in keys(D)
            D[q * q] = q
            return q, (q+1, D)
        else
            for p in D[q]
                if !(p+q in keys(D))
                    D[p+q] = []
                end
                push!(D[p+q], p)
            end
            delete!(D, q)
        end
    end
end
Base.done(::PrimeIt, q) = false
Base.iteratorsize(::PrimeIt) = Base.IsInfinite()
    
    
In [41]:
    
@timeit last(collect(take(PrimeIt(), 10001)))
    
    
    Out[41]:
In [8]:
    
bigString = replace("""73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450""", '\n', "")
    
    Out[8]:
In [35]:
    
@benchmark maximum([mapreduce(x->parse(Int64,x), *, 1, y) for y in [bigString[i:i+12] for i in 1:length(bigString)-12] if !('0' in y)])
    
    Out[35]:
In [48]:
    
[a*b*c for a in 1:1000 for b in a:1000 for c in b:1000 if (a^2 + b^2 == c^2 && a+b+c == 1000)]
    
    Out[48]:
In [51]:
    
@benchmark sum(PrimeSieve(2000000))
    
    Out[51]:
In [52]:
    
    
    Out[52]:
In [ ]: