MATLAB--Project Euler I

例1.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.

Thank you to Project Euler Problem 1

如果我们列出所有小于10的自然数中是3或5的倍数的数字,我们会得到3、5、6和9。这些倍数的和是23。

找出所有小于输入值的3或5的倍数的和。

感谢Project Euler问题1。”

以下是一个MATLAB函数来实现这个功能:

function sumMultiples = sumMultiplesOf3And5(inputValue)
    % 初始化总和
    sumMultiples = 0;
    
    % 循环遍历小于输入值的自然数
    for i = 1:inputValue-1
        % 如果是3或5的倍数,则加到总和中
        if mod(i, 3) == 0 || mod(i, 5) == 0
            sumMultiples = sumMultiples + i;
        end
    end
end

 然后,你可以将这个函数保存在一个名为sumMultiplesOf3And5.m的文件中,并在MATLAB中使用。例如:

inputValue = 10;
sumResult = sumMultiplesOf3And5(inputValue);
disp(['小于' num2str(inputValue) '的3或5的倍数的和是:' num2str(sumResult)]);

这将输出:

小于10的3或5的倍数的和是:23

例2.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.

斐波那契数列中的每一项都是前两项的和。通过从1和2开始,前10项为: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 考虑斐波那契数列中数值不超过输入值的项,找出其中偶数项的总和。

以下是一个MATLAB函数来实现这个功能:

function sumEvenFibonacci = sumEvenFibonacciTerms(inputValue)
    % 初始化斐波那契数列的前两项
    fibPrev = 1;
    fibCurr = 2;
    
    % 初始化总和
    sumEvenFibonacci = 0;
    
    % 循环生成斐波那契数列,并找出不超过输入值的偶数项的总和
    while fibCurr <= inputValue
        % 如果当前项是偶数,则加到总和中
        if mod(fibCurr, 2) == 0
            sumEvenFibonacci = sumEvenFibonacci + fibCurr;
        end
        
        % 生成下一项
        fibNext = fibPrev + fibCurr;
        fibPrev = fibCurr;
        fibCurr = fibNext;
    end
end

 然后,你可以将这个函数保存在一个名为sumEvenFibonacciTerms.m的文件中,并在MATLAB中使用。例如:

inputValue = 100;
sumResult = sumEvenFibonacciTerms(inputValue);
disp(['斐波那契数列中数值不超过' num2str(inputValue) '的偶数项的总和是:' num2str(sumResult)]);

这将输出:

斐波那契数列中数值不超过100的偶数项的总和是:44

例3.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?

Thank you to Project Euler Problem 3

“13195的质因数是5、7、13和29。

输入数字的最大质因数是多少?输入可能是uint64类型表示的大数字,输出必须是双精度。

感谢Project Euler的第3个问题。”

以下是一个MATLAB函数,用于找到输入数字的最大质因数:

function maxPrimeFactor = largestPrimeFactor(inputNumber)
    % Initialize maxPrimeFactor
    maxPrimeFactor = 0;
    
    % Loop through numbers from 2 to the square root of inputNumber
    for i = 2:sqrt(inputNumber)
        % Check if i is a factor of inputNumber
        if mod(inputNumber, i) == 0
            % Check if i is a prime number
            isPrime = true;
            for j = 2:sqrt(i)
                if mod(i, j) == 0
                    isPrime = false;
                    break;
                end
            end
            % If i is prime and a factor of inputNumber, update maxPrimeFactor
            if isPrime
                maxPrimeFactor = i;
            end
        end
    end
    
    % If maxPrimeFactor is still 0, inputNumber itself is prime
    if maxPrimeFactor == 0
        maxPrimeFactor = inputNumber;
    end
end

 这个函数接受一个输入数字,然后找到它的最大质因数,并返回结果。

例4.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.

Thank you to Project Euler Problem 4

回文数从左向右读和从右向左读是一样的。由两个两位数的乘积组成的最大回文数是9009 = 91 × 99。 找出由小于或等于输入数字的两个数相乘得到的最大回文数。 感谢Project Euler的第4个问题。

以下是一个 MATLAB 函数,用于找到由小于或等于输入数字的两个数相乘得到的最大回文数:

function maxPalindrome = largestPalindromeProduct(inputNumber)
    % Initialize maxPalindrome
    maxPalindrome = 0;
    
    % Loop through all possible combinations of two numbers less than or equal to inputNumber
    for i = 100:inputNumber
        for j = 100:inputNumber
            % Calculate the product
            product = i * j;
            % Check if the product is a palindrome and greater than the current maxPalindrome
            if isPalindrome(product) && product > maxPalindrome
                maxPalindrome = product;
            end
        end
    end
end

function result = isPalindrome(number)
    % Convert number to string
    strNumber = num2str(number);
    % Check if the string is equal to its reverse
    result = strcmp(strNumber, fliplr(strNumber));
end

这个函数接受一个输入数字,然后找到由小于或等于该数字的两个数相乘得到的最大回文数,并返回结果。

例5.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?

Thank you to Project Euler Problem 5

2520是可以被1到10中的每个数整除而没有余数的最小数。

什么是可以被1到输入数字中的所有数均匀地整除的最小正整数?

感谢Project Euler的第5个问题。

以下是一个 MATLAB 函数,用于找到可以被 1 到输入数字中的所有数均匀地整除的最小正整数:

function smallestDivisible = smallestMultiple(inputNumber)
    % Start with the input number as the smallest divisible number
    smallestDivisible = inputNumber;
    
    % Initialize found flag
    found = false;
    
    % Keep looping until a smallest divisible number is found
    while ~found
        % Assume the number is divisible until proven otherwise
        divisible = true;
        
        % Check divisibility by all numbers from 1 to inputNumber
        for i = 1:inputNumber
            if mod(smallestDivisible, i) ~= 0
                % If not divisible by any number, break the loop
                divisible = false;
                break;
            end
        end
        
        % If divisible by all numbers, set found flag to true
        if divisible
            found = true;
        else
            % Otherwise, increment the number and continue checking
            smallestDivisible = smallestDivisible + inputNumber;
        end
    end
end

这个函数接受一个输入数字,然后找到可以被 1 到该输入数字中的所有数均匀地整除的最小正整数,并返回结果。

例6.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.

Thank you to Project Euler Problem 6

前十个自然数的平方和为,

1^2 + 2^2 + ... + 10^2 = 385

前十个自然数的和的平方为,

(1 + 2 + ... + 10)^2 = 55^2 = 3025

因此,前十个自然数的平方和与和的平方之间的差为 3025 - 385 = 2640。

找到前 N(其中 N 是输入)个自然数的平方和与和的平方之间的差。

感谢 Project Euler 第 6 问题。

以下是一个 MATLAB 函数,用于找到前 N 个自然数的平方和与和的平方之间的差:

function difference = sumSquareDifference(N)
    % Calculate the sum of the squares of the first N natural numbers
    sumOfSquares = sum((1:N).^2);
    
    % Calculate the square of the sum of the first N natural numbers
    squareOfSum = sum(1:N)^2;
    
    % Calculate the difference
    difference = squareOfSum - sumOfSquares;
end

 这个函数接受一个输入参数 N,然后计算前 N 个自然数的平方和与和的平方之间的差,并返回结果。

例7.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?

Thank you to Project Euler Problem 7

“通过列出前六个质数:2、3、5、7、11 和 13,我们可以看到第 6 个质数是 13。 第 N 个质数是多少? 感谢 Project Euler 第 7 问题。

以下是一个 MATLAB 函数,用于找到第 N 个质数:

function nthPrime = findNthPrime(N)
    % Initialize variables
    primes = []; % Store prime numbers
    num = 2; % Start checking from 2
    while numel(primes) < N
        if isprime(num)
            primes = [primes num]; % Add prime number to the list
        end
        num = num + 1; % Move to the next number
    end
    nthPrime = primes(end); % Get the last prime number in the list, which is the Nth prime
end

 这个函数接受一个输入参数 N,然后找到第 N 个质数,并返回结果。

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

问题 246. 项目欧拉:问题 8,找到一个大字符串中的最大乘积

问题 246 并不是项目欧拉的问题编号,而是其他的问题。不过,我可以为你提供解决 Project Euler 问题 8 的 MATLAB 代码,该问题要求找到一个大数字字符串中连续的13个数字的最大乘积。

function maxProduct = findLargestProduct(numberString)
    % 将数字字符串转换为数字数组
    numbers = str2double(strsplit(numberString, ''));
    
    % 初始化最大乘积为0
    maxProduct = 0;
    
    % 设置连续数字的长度
    consecutiveDigits = 13;
    
    % 循环查找最大乘积
    for i = 1:(length(numbers) - consecutiveDigits + 1)
        % 获取连续的13个数字
        consecutiveNumbers = numbers(i:i+consecutiveDigits-1);
        
        % 计算这13个数字的乘积
        product = prod(consecutiveNumbers);
        
        % 更新最大乘积
        maxProduct = max(maxProduct, product);
    end
end

 可以使用这个函数来找到一个大数字字符串中连续的13个数字的最大乘积。例如:

numberString = '73167176531330624919225119674426574742355349194934...
                96983520312774506326239578318016984801869478851843...
                85861560789112949495459501737958331952853208805511...
                12540698747158523863050715693290963295227443043557...
                66896648950445244523161731856403098711121722383113...
                62229893423380308135336276614282806444486645238749...
                30358907296290491560440772390713810515859307960866...
                70172427121883998797908792274921901699720888093776...
                65727333001053367881220235421809751254540594752243...
                52584907711670556013604839586446706324415722155397...
                53697817977846174064955149290862569321978468622482...
                83972241375657056057490261407972968652414535100474...
                82166370484403199890008895243450658541227588666881...
                16427171479924442928230863465674813919123162824586...
                17866458359124566529476545682848912883142607690042...
                24219022671055626321111109370544217506941658960408...
                07198403850962455444362981230987879927244284909188...
                84580156166097919133875499200524063689912560717606...
                05886116467109405077541002256983155200055935729725...
                71636269561882670428252483600823257530420752963450';
maxProduct = findLargestProduct(numberString);
disp(['连续的13个数字的最大乘积是:' num2str(maxProduct)]);

这将输出连续的13个数字的最大乘积。

例9.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.

Thank you to Project Euler Problem 9.

Solve

勾股数是一组三个自然数,记作 a、b、c,满足以下条件: a^2 + b^2 = c^2 例如, 3^2 + 4^2 = 9 + 16 = 5^2 = 25。 存在唯一一组勾股数,满足 a + b + c = N(输入)。 找出它们的乘积 abc。 感谢 Project Euler 问题 9。

下面是一个 MATLAB 函数,用于找出满足条件的勾股数并计算它们的乘积 abc:

function product = pythagorean_triplet_product(N)
    % Initialize product
    product = -1; % If no such triplet found, return -1

    % Loop through possible values of a
    for a = 1:N
        % Loop through possible values of b
        for b = a:N
            % Calculate c
            c = sqrt(a^2 + b^2);
            
            % Check if c is a natural number
            if c == floor(c)
                % Check if the sum of a, b, and c equals N
                if a + b + c == N
                    % If a, b, and c form a Pythagorean triplet, calculate their product
                    product = a * b * c;
                    return; % Exit the function
                end
            end
        end
    end
end

 这个函数接受一个整数参数 N,表示要求的勾股数的总和。它会遍历可能的 a 和 b 值,并计算对应的 c 值,然后检查是否满足条件。如果找到满足条件的勾股数,则计算它们的乘积并返回。如果找不到符合条件的勾股数,则返回 -1。

例10.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.

Thank you Project Euler Problem 10

小于或等于 10 的素数之和是 2 + 3 + 5 + 7 = 17。 找出所有小于或等于输入 N 的素数的和。 感谢 Project Euler 问题 10。

下面是一个 MATLAB 函数,用于计算小于或等于给定输入 N 的所有素数的和:

function sum_primes = sum_of_primes(N)
    % Initialize the sum of primes
    sum_primes = 0;
    
    % Create a logical array "is_prime" to track primality
    is_prime = true(1, N);
    
    % 0 and 1 are not prime numbers, so mark them as false
    is_prime(1:2) = false;
    
    % Use the Sieve of Eratosthenes algorithm to find primes
    for num = 2:sqrt(N)
        if is_prime(num)
            % Mark multiples of num as not prime
            is_prime(num^2:num:N) = false;
        end
    end
    
    % Sum up all prime numbers
    sum_primes = sum(find(is_prime));
end

 这个函数接受一个整数参数 N,表示要求的素数的上限。它使用埃拉托斯特尼筛法(Sieve of Eratosthenes)来找出小于等于 N 的所有素数,并返回它们的和。

  • 16
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值