Project Euler Problems

Project Euler Problem 005

Description

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

main.ts

import { range } from "../util/util.ts";

function isDivisibleByEveryNumber(n: number, divisors: number[]): boolean {
    for (const divisor of divisors) {
        if (n % divisor !== 0) {
            return false
        }
    }

    return true
}

function main() {
    const divisors = range(1, 20).reverse()

    let i = 1;
    while (true) {
        if (i > 1_000_000_000) {
            break
        }

        if (isDivisibleByEveryNumber(i, divisors)) {
            console.log(i)
            break;
        }

        i += 1
    }
}

main()