Project Euler 001 – JavaScript

5 02 2011

Problem 1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Solution

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head>

    <title>Project Euler 001</title>

</head>

<body>

    <script type="text/javascript">

        //Add all the natural numbers below one

        //thousand that are multiples of 3 or 5.

        //this one is pretty similar to c#

        answer = 0

        for (three = 0; three < 1000; three += 3) { answer += three; }

        for (five = 0; five < 1000; five += 5) { answer += five; }

        for (fifteen = 0; fifteen < 1000; fifteen += 15)

          { answer -= fifteen; }

        document.write("<b>" + answer + "</b>");

    </script>

</body>

</html>

 

Discussion

Last solution of the week.    Nothing too special here.

If you have questions, leave a comment or find me on Twitter (@azzlsoft) or email (rich@azzlsoft.com).


Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: