Julia Language - Euler Project

Posted at — Jul 10, 2022

Project Euler 8: Largest product in a series

function greatest_product_of_consecutive_digits(array::Array{Int}, k::Int)::Int
    product = prod(array[1:k])
    cache = array[1]
    maximum_prod = product

    for i in 1:(length(array)-k)
        if cache != 0
            product = div(product, cache) * array[k+i]
        else
            product = prod(array[1+i:k+i])
        end
        cache = array[1+i]
        if product > maximum_prod
            maximum_prod = product
        end
    end
    return maximum_prod
end

function main()
    t = parse(Int64, readline())
    for i in 1:t
        n, k = map((x) -> parse(Int64, x), split(readline(), " "))
        arr_number = [parse(Int, e) for e in readline()]
        result = greatest_product_of_consecutive_digits(arr_number, k)
        println(result)
    end
end

main()

Project Euler 7: 10001st prime

function compute_prime_number(n_element::Int)::Array{Int}
    array_prime_number = Int[2]
    a = 3

    while length(array_prime_number) < n_element
        is_prime::Bool = true
        for e in array_prime_number
            if a % e == 0
                is_prime = false
                break
            end
        end

        if is_prime
            push!(array_prime_number, a)
        end
        a += 1
    end

    return array_prime_number
end


function main()
    prime_table = compute_prime_number(10_000)

    t = parse(Int64, readline())
    for i in 1:t
        n = parse(Int64, readline())
        result = prime_table[n]
        println(result)
    end
end

main()

Project Euler 6: Sum square difference

function solution_euler_006(n::Int64)::Int64
    return div((3 * n^4 + 2 * n^3 - 3 * n^2 - 2 * n), 12)
end

function main()
    t = parse(Int64, readline())
    for i in 1:t
        n = parse(Int64, readline())
        result = solution_euler_006(n)
        println(result)
    end
end
main()

Project Euler 5: Smallest multiple

function solution_euler_005(n::Int64)::Int64
    smallest_multiple = 1

    for i in 1:n
        current_gcd = gcd(smallest_multiple, i)
        unique_1 = div(smallest_multiple, current_gcd)
        unique_2 = div(i, current_gcd)
        smallest_multiple = unique_1 * current_gcd * unique_2
    end
    
    return smallest_multiple
end

function main()
    t = parse(Int64, readline())
    for i in 1:t
        n = parse(Int64, readline())
        result = solution_euler_005(n)
        println(result)
    end
end

main()

Project Euler 4: Largest palindrome product

function solution_euler_004(n::Int64)::Int64
    max_palind_prod::Int64 = -1
    for i in 1:1000
        for j in 1:1000
            if i <= j
                p_n = i*j
                string_p_n = string(p_n)
                if p_n < n && p_n > max_palind_prod && string_p_n == reverse(string_p_n)
                    max_palind_prod = p_n
                end
            end
        end
    end
    return max_palind_prod
end

function main()
    t = parse(Int64, readline())
    for i in 1:t
        n = parse(Int64, readline())
        result = solution_euler_004(n)
        println(result)
    end
end

main()

Project Euler 3: Largest prime factor

function solution_euler_003(n::Int64)::Int64
    @assert n > 1 "Wrong input, n > 1"
    # set dummy variable
    largest_prime_factor::Int64 = 0

    while n % 2 == 0
        n = n >> 1
        largest_prime_factor = 2
    end

    while n % 3 == 0
        n = div(n, 3)
        largest_prime_factor = 3
    end

    for i in 5:6:convert(Int64, trunc(sqrt(n)))
        while n % i == 0
            n = div(n, i)
            largest_prime_factor = i
        end
        i_2 = i + 2
        while n % i_2 == 0
            n = div(n, i_2)
            largest_prime_factor = i_2
        end
    end

    if n > 1
        largest_prime_factor = n
    end

    return largest_prime_factor
end

function main()
    t = parse(Int64, readline())
    for i in 1:t
        n = parse(Int64, readline())
        result = solution_euler_003(n)
        println(result)
    end
end

main()

Project Euler 2: Even Fibonacci numbers

function solution_euler_002(n::Int)::Int
    sum_of_the_even_valued_fib::Int = 0
    a_n::Int = 1
    a_n_1::Int = 2
    i::Int = 0
    
    while a_n_1 <= n
        if i > 0
            a_n, a_n_1 = a_n_1, a_n + a_n_1
        end

        if a_n % 2 == 0
            sum_of_the_even_valued_fib += a_n
        end
        
        i+=1
    end

    return sum_of_the_even_valued_fib
end

function main()
    t = parse(Int, readline())
    for i = 1:t
        n = parse(Int, readline())
        result = solution_euler_002(n)
        println(result)
    end
end

main()

Project Euler 1: Multiples of 3 and 5

sum_arithmetic_series(a_1::Int, a_n::Int, n::Int)::Int = div(n * (a_1 + a_n), 2)

function solution_euler_001(n::Int)::Int
    # Below n
    n -= 1
    sum_of_all_the_multiples::Int = 0

    sum_of_all_the_multiples += sum_arithmetic_series(0, n - n % 3, div(n, 3) + 1)
    sum_of_all_the_multiples += sum_arithmetic_series(0, n - n % 5, div(n, 5) + 1)
    sum_of_all_the_multiples -= sum_arithmetic_series(0, n - n % 15, div(n, 15) + 1)
    return sum_of_all_the_multiples
end

function main()
    t = parse(Int, readline())
    for i = 1:t
        n = parse(Int, readline())
        result = solution_euler_001(n)
        println(result)
    end
end

main()