diff --git a/ProjectEuler/Problem1.m b/ProjectEuler/Problem1.m new file mode 100644 index 0000000..26171ee --- /dev/null +++ b/ProjectEuler/Problem1.m @@ -0,0 +1,25 @@ +%ProjectEuler/Problem1.m +%This is a script to answer Problem 1 for Project Euler +%What is the sum of all the multiples of 3 or 5 that are less than 1000 + +%Setup your variables +fullSum = 0; %To hold the sum of all the numbers +numbers = 0; %To hold all of the numbers +counter = 0; %The number. It must stay below 1000 + +while(counter < 1000) + %See if the number is a multiple of 3 + if(mod(counter, 3) == 0) + numbers(end + 1) = counter; + %See if the number is a multiple of 5 + elseif(mod(counter, 5) == 0) + numbers(end + 1) = counter; + end + + %Increment the number + ++counter; +end +%When done this way it removes the possibility of duplicate numbers + +fullSum = sum(numbers); +fullSum diff --git a/ProjectEuler/Problem2.m b/ProjectEuler/Problem2.m new file mode 100644 index 0000000..72b827a --- /dev/null +++ b/ProjectEuler/Problem2.m @@ -0,0 +1,24 @@ +%ProjectEuler/Problem2.m +%This is a script to answer Problem 2 for Project Euler +%The sum of the even Fibonacci numbers less than 4,000,000 + +%Setup your Variables +fib = [1, 1, 2]; %Holds the Fibonacci numbers +currentFib = fib(end) + fib(end - 1); %The current Fibonacci number to be tested +evenFib = [2]; %A subset of the even Fibonacci numbers +finalSum = 0; + +while(currentFib < 4000000) + %Add the number to the list + fib(end + 1) = currentFib; + %If the number is even add it to the even list as well + if(mod(currentFib, 2) == 0) + evenFib(end + 1) = currentFib; + end + + %Set the next Fibonacci + currentFib = fib(end) + fib(end - 1); +end + +finalSum = sum(evenFib); +finalSum diff --git a/ProjectEuler/Problem3.m b/ProjectEuler/Problem3.m new file mode 100644 index 0000000..a61621f --- /dev/null +++ b/ProjectEuler/Problem3.m @@ -0,0 +1,47 @@ +%ProjectEuler/Problem3.m +%This is a script to answer Problem 3 for Project Euler +%The largest prime factor of 600851475143 + +%Setup your variables +number = 600851475143; %The number we are trying to find the greatest prime factor of +primeNums = []; %A list of prime numbers. Will include all prime numbers <= number +factors = []; %For the list of factors of number +tempNum = number; %Used to track the current value if all of the factors were taken out of number +%number = 16; %Used for a test case + +%Get the prime numbers up to sqrt(number). If it is not prime there must be a value <= sqrt +primeNums = primes(sqrt(number)); + +%Setup the loop +counter = 1; +%Start with the lowest number and work your way up. When you reach a number > size(primeNums) you have found all of the factors +while(counter <= size(primeNums)(2)) + + %Divide the number by the next prime number in the list + answer = (tempNum/primeNums(counter)); + + %If it is a whole number add it to the factors + if(mod(answer,1) == 0) + factors(end + 1) = primeNums(counter); + %Set tempNum so that it reflects number/factors + tempNum = tempNum / primeNums(counter); + %Keep the counter where it is in case a factor appears more than once + %Get the new set of prime numbers + primeNums = primes(sqrt(tempNum)); + else + %If it was not an integer increment the counter + ++counter; + end +end +%When the last number is not divisible by a prime number it must be a prime number +factors(end + 1) = tempNum; + +%Remove the variables +clear counter; +clear tempNum; +clear answer; +clear number; +clear primeNums; + +%Print the answer +max(factors)