From charlesreid1

No edit summary
Line 1: Line 1:
This is problem 1 on Project Euler: https://projecteuler.net/problem=1
This is problem 1 on Project Euler: https://projecteuler.net/problem=1
=Overview of the Problem=


==Question==
==Question==
Line 7: Line 9:
Find the sum of all the multiples of 3 or 5 below 1000.
Find the sum of all the multiples of 3 or 5 below 1000.


(This is essentially the fizz buzz problem, but with a practical application.)
Note that, if you've ever seen the fizz-buzz problem in computer science, this is the number-theory version.
 
==Why this problem==
 
Naturally, the very first Project Euler question will raise the question - why this one? Project Euler is a fantastic, complex, and difficult site involving many beautiful aspects of number theory. So why begin the journey at this problem in particular?
 
This problem shares many similarities, algorithm-wise, with the Sieve of Eratosthenes (at least, that's the idea.) With the Sieve of Eratosthenes, you begin with the smallest primes and work your way up by finding multiples of these primes and crossing them off the list of potential primes. In essence, ''this is the first question because it is the most fundamental operation of the most fundamental algorithm in number theory.''


==Approach==
=Solution=


Two-part problem: enumerating multiples of 3 or 5, and summing them up.
We can come up with a simple solution that uses a bit vector to represent integers, or we can toss numbers into a set (which, under the hood, is likely using a similar representation anyway). Given our maximum N, and our two numbers a and b, we can allocate a boolean array with that many integers, set them all to false, then walk through the list in intervals of 3 and 5, flipping the booleans to true as we go.


Data structure:
==Going Deeper==
* Use a set to store integers and avoid duplicates.


Task 1: generate all multiples of b, up to a given maximum N.
It's true that this problem seems a bit boring on its face. But let's dive deeper. Suppose I asked you to find the number of multiples of the integers 3 and 4, but not the integer 5, below 2,000 - and to do so without explicitly enumerating them with a computer.
* Given a maximum N, biggest number t hat can be a multiple of b is N/b.
* Generate numbers from 1 to N/b.
* Multiply them by b to generate numbers up to N that are multiples.


Task 2: sum them up.
To do this, we can express the problem in set notation. We have three sets, A, B, C, containing multiples of 3, 4, and 5, respectively. In set theory language, we wish to find
* Iterate over your set using a generator.


==Code==
<math>
( A \bigcup B ) \ C
</math>


<pre>
import java.util.Set;
import java.util.HashSet;


public class Euler {
public static void main(String[] args) {
// Maximum
int max = 100;
// Multiples list
int[] multiplesList = {3,5};
// Results set
Set<Integer> s = new HashSet<Integer>();


// From 1 up to and including (max-1)/multiple (not max/multiple-1)
for(int m=0; m<multiplesList.length; m++) {
int multiple = multiplesList[m];
for(int i=1; i<=((max-1)/multiple); i++) {
int num = i*multiple;
s.add(num);
}
}
// Sum them all up
int sum = 0;
for(Integer i : s) {
sum += i;
}
System.out.println("sum = "+sum);
}


}
</pre>





Revision as of 09:01, 20 July 2017

This is problem 1 on Project Euler: https://projecteuler.net/problem=1

Overview of the Problem

Question

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.

Note that, if you've ever seen the fizz-buzz problem in computer science, this is the number-theory version.

Why this problem

Naturally, the very first Project Euler question will raise the question - why this one? Project Euler is a fantastic, complex, and difficult site involving many beautiful aspects of number theory. So why begin the journey at this problem in particular?

This problem shares many similarities, algorithm-wise, with the Sieve of Eratosthenes (at least, that's the idea.) With the Sieve of Eratosthenes, you begin with the smallest primes and work your way up by finding multiples of these primes and crossing them off the list of potential primes. In essence, this is the first question because it is the most fundamental operation of the most fundamental algorithm in number theory.

Solution

We can come up with a simple solution that uses a bit vector to represent integers, or we can toss numbers into a set (which, under the hood, is likely using a similar representation anyway). Given our maximum N, and our two numbers a and b, we can allocate a boolean array with that many integers, set them all to false, then walk through the list in intervals of 3 and 5, flipping the booleans to true as we go.

Going Deeper

It's true that this problem seems a bit boring on its face. But let's dive deeper. Suppose I asked you to find the number of multiples of the integers 3 and 4, but not the integer 5, below 2,000 - and to do so without explicitly enumerating them with a computer.

To do this, we can express the problem in set notation. We have three sets, A, B, C, containing multiples of 3, 4, and 5, respectively. In set theory language, we wish to find

$ ( A \bigcup B ) \ C $