aboutsummaryrefslogtreecommitdiff
path: root/julia/012-highly_divisible_triangular_number.jl
blob: 92496f870fd4050c043741a2a237666dfd78cd43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
###
# Highly divisible triangular number
# Problem 12
#
# The sequence of triangle numbers is generated by adding the natural numbers. So the 7th
# triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
# 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
# Let us list the factors of the first seven triangle numbers:
#  1: 1 3: 1,3 6: 1,2,3,610: 1,2,5,1015: 1,3,5,1521: 1,3,7,2128: 1,2,4,7,14,28
# We can see that 28 is the first triangle number to have over five divisors.
# What is the value of the first triangle number to have over five hundred divisors?
###


using Base.Iterators


# Eddie woo factors: https://www.youtube.com/watch?v=KRfv9pQCTDE
#
# we can skip the numbers after the square root since factors
# come in pairs reflected at the square root
# e.g: 24: 1, 2, 3, 4, 6, 8, 12, 24
#      1 - 24
#      2 - 12
#      3 - 8
#      4 - 6
#
# all non prime numbers have an even number of factors except for the squares
# since they have a "pair" that is the same number
# e.g 16: 1, 2, 4, 8, 16
#     1 - 16
#     2 - 8
#     4 - 4 <<<
#
# square numbers (i.e 4, 9, 16, 25, etc) have 3 divisors unless their root is also a square number
# 4: 1, 2, 4 (2 is prime)
# 9: 1, 3, 9 (3 is prime)
# 16: 1, 2, 4, 8, 16 (4 is square)

function divisor_count(n)
    count = 0
    s = 0
    while true
        s = floor(Integer, √n)
        (n != 1 && s * s == n) || break
        count += 1
        n = s
    end
    for d in 1:s
        if n % d == 0
            count += 2
        end
    end
    return count
end

total = 0
for n in countfrom(1)
    global total += n
    if divisor_count(total) > 500
        println(total)
        break
    end
end

# can't do  countfrom |> cumsum |> takewhile |> last because cumsum doesn't support infinite iterators