Project Euler: Problem 1

Problem: Find the sum of all numbers below 1000 which are a multiple of 3 or 5.

Solution: Given the numbers involved, we can simply brute-force the answer by simply checking each number below 1000 for divisibility by 3 or 5 and keep a running total.

<?php

declare(strict_types=1);
error_reporting(E_ALL);

$sum = 0;

for ($num = 1; $num < 1000; $num++)
{
    if ($num % 3 === 0 || $num % 5 === 0)
    {
        $sum += $num;
    }
}

print("$sum\n");

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.