Project Euler I Matlab Cody Solutions

Project Euler 1 Cody solution

Learn Matlab and earn a badge and skill. This section contains the Cody solution of Project-Euler 1 Cody Solution. Total 10/10 solved problems.

Visit the Matlab official Problems page of Project-Euler 1. 

All solutions were verified and working. It may be possible after a year or more community may modify the question, then the provided solution may not work so do comment below about the same. Our team will suddenly work on it.

JOIN US ON TELEGRAM
Get Notified Faster

Problem 230. Project Euler: Problem 1, Multiples of 3 and 5

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 the input value.
function y = euler001(x)
    y = sum(3:3:x-1)+sum(5:5:x-1)-sum(15:15:x-1);
end

Problem 232. Project Euler: Problem 2, Sum of even Fibonacci

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not exceed the input value, find the sum of the even-valued terms.
function y = euler002(x)
    f = [1 2];
    while f(end)+f(end-1)<=x
        f = [f f(end)+f(end-1)];
    end
    y = sum(f(find(mod(f,2)==0)));
end

Problem 234. Project Euler: Problem 3, Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number being input, input might be uint64 for large numbers, out must be double precision?
function y = euler003(x)
    y=max(factor(uint64(x)));
end

Problem 235. Project Euler: Problem 4, Palindromic numbers

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of numbers less than or equal to the input number.
function y = euler004(x)
    y = 0;
    for i = max(1, x-100):x
        for j = i:x
            prod = i * j;
            prodstr = num2str(prod);
            if prodstr == reverse(prodstr)
                y = max(y, prod);
            end
        end
    end
end

Problem 239. Project Euler: Problem 5, Smallest multiple

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 input number?
function y = euler005(x)
    y = 1;
    for k=2:x
        y = lcm(y,k);
    end
end

Problem 240. Project Euler: Problem 6, Natural numbers, squares and sums.

The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + … + 10^2 = 385 The square of the sum of the first ten natural numbers is,
(1 + 2 + … + 10)^2 = 55^2 = 3025 Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 – 385 = 2640.
Find the difference between the sum of the squares of the first N (where N is the input) natural numbers and the square of the sum.
function y = euler006(x)
  y = (3*x^4+2*x^3-3*x^2-2*x)/12;
end

Problem 241. Project Euler: Problem 7, Nth prime

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the Nth prime number?
function y = euler007(x)
    p = primes(2000000);
    y=p(x);
end

Problem 246. Project Euler: Problem 8, Find largest product in a large string of numbers

Find the greatest product of five consecutive digits in an n-digit number.
 
The large number will be given as a string, 1xn characters.
function y = euler008(x)
    y = 0;
    x(1000)
    for i=1:length(x)-4
        b = str2num(x(i))*str2num(x(i+1))*str2num(x(i+2))*str2num(x(i+3))*str2num(x(i+4));
        if b>y
            y=b;
        end
    end
end

Problem 249. Project Euler: Problem 9, Pythagorean numbers

A Pythagorean triplet is a set of three natural numbers, a b c, for which,
a^2 + b^2 = c^2
For example,
3^2 + 4^2 = 9 + 16 = 5^2 = 25.
There exists exactly one Pythagorean triplet for which a + b + c = N (the input).
Find the product abc.
function y = euler009(x)
for i=1:x
    for j=i:x
        if i^2+j^2==(x-i-j)^2
            y=i*j*(x-i-j)
        end
    end
end
end

Problem 250. Project Euler: Problem 10, Sum of Primes

The sum of the primes less than or equal to 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes less than or equal to the input, N.
function y = euler010(x)
  y = sum(primes(x));
end

Project Euler 1  10/10 solved problems. All solution is correct as they were first submitted in Matlab and then uploaded here for your help. If any solution doesn’t work then do comment.

Also Check the solution of other Matlab Cody Contest Matlab Cody More Contest solutions.

Leave a Comment

Your email address will not be published.

Tech Info