MATLAB--Project Euler II

例1.Problem 42935. Sums of cubes and squares of sums

Given the positive integers 1:n, can you:

1. Compute twice the sum of the cubes of those numbers.
2. Subtract the square of the sum of those numbers.
3. Divide that result by n/2. 

So, for n = 3, we might compute a result like this:

((1^3 + 2^3 + 3^3)*2 - (1 + 2 + 3)^2)/(3/2)
ans =
    24

Yes, you probably can do all of this, but be careful on this problem, as n may be somewhat large, and I am expecting to see the correct result, not just an approximate value. Remember there are always different ways one may solve a problem.

I point out the Project Euler reference because PE problem 6 is what made me think of this problem, and because the test cases will push the limits of what you can do if you are not careful.

给定正整数 1:n,你能做到以下几点吗:

  1. 计算这些数的立方和的两倍。
  2. 减去这些数的和的平方。
  3. 将结果除以 n/2。

因此,对于 n = 3,我们可以计算出一个类似这样的结果:

((1^3 + 2^3 + 3^3)*2 - (1 + 2 + 3)^2)/(3/2) 答案为: 24

是的,你可能能够完成这些步骤,但在解决这个问题时需要小心,因为 n 可能会比较大,我希望看到正确的结果,而不仅仅是一个近似值。请记住,解决问题总有不同的方法。

我提到了 Project Euler 的参考,是因为 PE 问题6让我想起了这个问题,并且测试案例将会挑战你的能力,如果你不小心的话。

以下是用MATLAB编写的程序,用于计算给定正整数 1:n 的要求:

function result = calculateResult(n)
    numbers = 1:n;
    sum_of_cubes = sum(numbers.^3);
    square_of_sum = sum(numbers)^2;
    result = (2 * sum_of_cubes - square_of_sum) / (n/2);
end

% 测试 n = 3
n = 3;
result = calculateResult(n)

例2.Problem 2337. Sum of big primes without primes

Inspired by Project Euler n°10 (I am quite obviously a fan).

With problem n°250 by Doug, you can find some global methods to compute the sum of all the primes below the input n.

For example, the sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

But how to proceed (in time) with big number and WITHOUT the primes function ?

HINTS: sum(primes(n)) is possible here but why miss the wonderfull Sieve of Eratosthenes ?

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

问题 2337. 没有使用 primes 函数的大质数之和

受到欧拉项目第10题的启发(显然,我是一个粉丝)。

通过 Doug 的第250题,你可以找到一些计算小于输入 n 的所有质数之和的全局方法。

例如,小于 10 的质数之和是 2 + 3 + 5 + 7 = 17。

但是如何在不使用 primes 函数的情况下以及处理大的数值呢?

提示:sum(primes(n)) 在这里是可行的,但为什么要错过 Eratosthenes 筛法的奇妙呢?

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

以下是用MATLAB编写的程序,用于计算小于给定输入n的所有质数之和,而不使用primes函数:

function primeSum = sumOfPrimes(n)
    % 初始化筛选数组
    sieve = true(1, n);
    sieve(1) = false; % 1不是质数

    % 使用筛法找到质数
    for i = 2:sqrt(n)
        if sieve(i)
            % 将i的倍数标记为非质数
            sieve(i*i:i:n) = false;
        end
    end

    % 计算质数之和
    primes = find(sieve);
    primeSum = sum(primes);
end

% 测试 n = 10
n = 10;
primeSum = sumOfPrimes(n)

例3.Problem 42936. Project Euler: Problem 11, Largest product in a grid

What is the greatest product of k adjacent numbers in the same direction (up, down, left, right, or diagonally) in a n×n grid ?

Project Euler Problem 11

问题 42936. 欧拉项目:问题 11,网格中的最大乘积

在一个 n×n 的网格中,沿同一方向(上、下、左、右或对角线)连续 k 个数字的乘积最大是多少?

欧拉项目问题 11

以下是用MATLAB编写的程序,用于解决欧拉项目问题11:在一个n×n的网格中,找出沿同一方向(上、下、左、右或对角线)连续k个数字的最大乘积。

function maxProduct = largestProductInGrid(grid, k)
    [rows, cols] = size(grid);
    maxProduct = 0;
    
    % 水平方向
    for i = 1:rows
        for j = 1:cols-k+1
            product = prod(grid(i, j:j+k-1));
            maxProduct = max(maxProduct, product);
        end
    end
    
    % 垂直方向
    for i = 1:rows-k+1
        for j = 1:cols
            product = prod(grid(i:i+k-1, j));
            maxProduct = max(maxProduct, product);
        end
    end
    
    % 对角线方向(左上到右下)
    for i = 1:rows-k+1
        for j = 1:cols-k+1
            product = prod(diag(grid(i:i+k-1, j:j+k-1)));
            maxProduct = max(maxProduct, product);
        end
    end
    
    % 对角线方向(右上到左下)
    for i = 1:rows-k+1
        for j = k:cols
            product = prod(diag(fliplr(grid(i:i+k-1, j-k+1:j))));
            maxProduct = max(maxProduct, product);
        end
    end
end

% 测试数据
grid = [...
    8  2 22 97 38 15  0 40  0 75;
    49 49 99 40 17 81 18 57 60 87;
    81 49 31 73 55 79 14 29 93 71;
    52 70 95 23  4 60 11 42 69 24;
    22 31 16 71 51 67 63 89 41 92;
    24 47 32 60 99 03 45 02 44 75;
    32 98 81 28 64 23 67 10 26 38;
    67 26 20 68 02 62 12 20 95 63;
    24 55 58 05 66 73 99 26 97 17;
    21 36 23 09 75 00 76 44 20 45];
k = 4;

maxProduct = largestProductInGrid(grid, k)

你可以将上述代码复制到MATLAB环境中运行以进行测试。它接受一个n×n的网格和一个整数k作为输入,并返回沿同一方向连续k个数字的最大乘积。在上面的示例中,我们使用了一个10×10的网格和k=4。你可以根据需要修改输入来测试其他情况。 

例4.Problem 44732. Highly divisible triangular number (inspired by Project Euler 12)

Triangular numbers can be calculated by the sum from 1 to n. For example, the first 10 triangular numbers are:

 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

All divisors for each of these numbers are listed below

 1: 1
 3: 1,3
 6: 1,2,3,6
 10: 1,2,5,10
 15: 1,3,5,15
 21: 1,3,7,21
 28: 1,2,4,7,14,28
 36: 1,2,3,4,6,9,12,18,36
 45: 1,3,5,9,15,45
 55: 1,5,11,55

Your challenge is to write a function that will return the value of the first triangular number to have over d divisors (d will be passed to your function).

问题44732。高度可整除的三角形数(灵感来自欧拉项目12)

三角形数可以通过从1加到n来计算。例如,前10个三角形数是:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... 每个这些数字的所有因子如下列出:

1: 1 3: 1,3 6: 1,2,3,6 10: 1,2,5,10 15: 1,3,5,15 21: 1,3,7,21 28: 1,2,4,7,14,28 36: 1,2,3,4,6,9,12,18,36 45: 1,3,5,9,15,45 55: 1,5,11,55 你的挑战是编写一个函数,该函数将返回具有超过d个因子的第一个三角形数的值(d将传递给您的函数)。

以下是用MATLAB编写的函数,用于解决问题44732:寻找具有超过d个因子的第一个三角形数的值。

function result = highlyDivisibleTriangularNumber(d)
    n = 1;
    while true
        triangularNumber = n*(n+1)/2;
        factors = factor(triangularNumber);
        numFactors = length(unique(factors));
        if numFactors > d
            result = triangularNumber;
            break;
        end
        n = n + 1;
    end
end

你可以将上述代码复制到MATLAB环境中运行以进行测试。该函数接受一个整数d作为输入,并返回具有超过d个因子的第一个三角形数的值。例如,如果你想找到具有超过5个因子的第一个三角形数,可以调用 highlyDivisibleTriangularNumber(5)

例5.Problem 2664. Divisors for big integer

Inspired by Problem 1025 and Project Euler 12.

Given n, return the number y of integers that divide N.

For example, with n = 10, the divisors are [1 2 5 10], so y = 4.

It's easy with normal integer but how to proceed with big number?

问题2664。大整数的因子

灵感来自问题1025和欧拉项目12。

给定n,返回能整除N的整数数量y。

例如,当n = 10时,因子为[1 2 5 10],因此y = 4。

对于普通整数很容易,但如何处理大数呢?

下面是一个用MATLAB编写的函数,用于计算大整数n的因子数量:

function y = countDivisors(n)
    factors = factor(n);
    y = length(factors);
end

可以将上述代码保存到一个名为"countDivisors.m" 的文件中。然后,在MATLAB环境中,你可以调用这个函数并传入一个大整数n来计算它的因子数量。例如,你可以使用以下命令来计算 n = 10 的因子数量:

result = countDivisors(10);
disp(result); % 显示结果

 这段代码会将计算结果打印出来,对于 n = 10,结果是 4。这个函数利用MATLAB内置的 factor 函数来计算大整数n的因子,非常方便实用。

例6.Problem 44733. Large Sum (inspired by Project Euler 13)

Your function will be provided an arbitrary number of numbers of arbitrary sizes as a cell array of strings. Some numbers will be very large. The function must return the first eight digits of the sum of all the numbers as an integer. 

问题44733。大数求和(灵感来自欧拉项目13)

你的函数将以字符串的cell数组形式提供任意数量、任意大小的数字。其中一些数字会非常大。函数必须返回所有这些数字的和的前八位数字作为一个整数。

下面是一个用MATLAB编写的函数,用于计算大数的前八位数字和:

function result = largeSum(digits)
    total = 0;
    for i = 1:length(digits)
        total = total + str2double(digits{i});
    end
    result = str2double(extractBefore(num2str(total), 9));
end

你可以将上述代码保存到一个名为"largeSum.m" 的文件中。然后,在MATLAB环境中,你可以调用这个函数并传入一个数字的字符串cell数组来计算它们的和的前八位数字。例如,你可以使用以下命令来计算数字 {"123456789", "987654321"} 的和的前八位数字:

result = largeSum({"123456789", "987654321"});
disp(result); % 显示结果

 这段代码会将计算结果打印出来。在这个例子中,前八位数字的和是 111111110。

例7.Problem 42673. Longest Collatz Sequence

Inspired by Projet Euler n°14.

The Collatz iterative sequence (See Cody problem n° 2103 and 211) is defined for the set of positive integers:

  • n → n/2 (n is even)
  • n → 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under number given in input, produces the longest chain?

Be smart because numbers can be big...

问题42673。最长的Collatz序列

灵感来自欧拉项目14。

Collatz迭代序列(参见Cody问题n° 2103和211)定义为正整数集合:

n → n/2(n为偶数) n → 3n + 1(n为奇数) 使用上述规则,从13开始,我们生成以下序列:

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

可以看出,这个序列(从13开始,以1结束)包含10个项。虽然尚未被证明(Collatz问题),但人们认为所有起始数字最终都会到达1。

在输入给定的数字下,哪个起始数字产生的链最长?

要聪明一点,因为数字可能很大...

以下是一个可能的MATLAB函数来解决这个问题:

function longestCollatzSequence(limit)
    maxChainLength = 0;
    startingNumber = 0;
    
    for i = 1:limit
        currentNumber = i;
        chainLength = 1;
        
        while currentNumber ~= 1
            if mod(currentNumber, 2) == 0
                currentNumber = currentNumber / 2;
            else
                currentNumber = 3 * currentNumber + 1;
            end
            chainLength = chainLength + 1;
        end
        
        if chainLength > maxChainLength
            maxChainLength = chainLength;
            startingNumber = i;
        end
    end
    
    disp(['在输入的数字下,产生最长链的起始数字是:', num2str(startingNumber)]);
end

你可以调用这个函数并传入一个数字来计算哪个起始数字产生的Collatz序列链最长。例如,你可以使用以下命令来找出在输入数字下,哪个起始数字产生的链最长:

longestCollatzSequence(1000000);

 这将计算从1到1000000之间的所有数中,哪个起始数字产生的Collatz序列链最长,并打印出结果。

例8.Problem 252. Project Euler: Problem 16, Sums of Digits of Powers of Two

2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 2^N?

Thanks to Project Euler Problem 16.

问题252. 欧拉项目:问题16,2的幂的数字之和

2的15次方等于32768,其数字之和为3 + 2 + 7 + 6 + 8 = 26。

对于数字2的N次方,其数字之和是多少?

感谢欧拉项目问题16。

你可以使用以下 MATLAB 代码来计算数字 2 的 N 次方的数字之和:

function sumOfDigitsOfPowerOfTwo(N)
    result = num2str(2^N);
    sumOfDigits = sum(str2num(result'));
    disp(['数字2的', num2str(N), '次方的数字之和为:', num2str(sumOfDigits)]);
end

你可以调用这个函数并传入一个数字 N 来计算数字 2 的 N 次方的数字之和。例如,你可以使用以下命令来计算数字 2 的 15 次方的数字之和:

sumOfDigitsOfPowerOfTwo(15);

 这将计算 2 的 15 次方的数字之和,并打印出结果。

例9. Problem 42938. Project Euler: Problem 18, Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

       3*
     7*  4
   2   4*  6
 8   5   9*  3

3 + 7 + 4 + 9 = 23

Find the maximum total from top to bottom of a given triangle.

问题42938. 欧拉项目:问题18,最大路径和 I

通过从下面的三角形顶部开始,并向下一行中的相邻数字移动,从顶部到底部的最大总和是23。

     3*
   7*  4
 2   4*  6
8   5   9*  3

3 + 7 + 4 + 9 = 23

找出给定三角形从顶部到底部的最大总和。

你可以使用以下 MATLAB 代码来解决这个问题:

function maxPathSum(triangle)
    % 从底部向上迭代计算每个位置的最大路径和
    for i = size(triangle, 1) - 1:-1:1
        for j = 1:i
            % 选择较大的相邻数字并相加
            triangle(i, j) = triangle(i, j) + max(triangle(i+1, j), triangle(i+1, j+1));
        end
    end
    
    % 最终最大路径和存储在三角形顶部
    disp(['给定三角形从顶部到底部的最大总和为:', num2str(triangle(1, 1))]);
end

你可以调用这个函数并传入一个表示三角形的矩阵来找出给定三角形从顶部到底部的最大总和。例如,将下面的三角形表示为一个矩阵,并使用以下命令来找出最大总和:

triangle = [3, 0, 0, 0; 7, 4, 0, 0; 2, 4, 6, 0; 8, 5, 9, 3];
maxPathSum(triangle);

 这将计算给定三角形从顶部到底部的最大总和,并打印出结果。

例10.Problem 273. Recurring Cycle Length (Inspired by Project Euler Problem 26)

Preface: This problem is inspired by Project Euler Problem 26 and uses text from that question to explain what a recurring cycle is.

Description

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:

1/2 = 0.5

1/3 = 0.(3)

1/4 = 0.25

1/5 = 0.2

1/6 = 0.1(6)

1/7 = 0.(142857)

1/8 = 0.125

1/9 = 0.(1)

1/10 = 0.1

Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.

Create a function that can determine the length of the recurring cycle of 1/d given d.

问题273. 重复循环长度(灵感来自欧拉项目问题26)

前言:这个问题受到了欧拉项目问题26的启发,并使用了该问题中的文字来解释重复循环是什么。

描述

单位分数的分子为1。单位分数的小数表示形式,分母从2到10分别如下:

1/2 = 0.5

1/3 = 0.(3)

1/4 = 0.25

1/5 = 0.2

1/6 = 0.1(6)

1/7 = 0.(142857)

1/8 = 0.125

1/9 = 0.(1)

1/10 = 0.1

这里,0.1(6) 表示 0.166666...,它有一个1位数的循环。可以看出,1/7 有一个6位数的循环。

创建一个函数,可以确定给定分母 d 的 1/d 的重复循环长度。

以下是一个用 MATLAB 编写的函数,用于确定给定分母 d 的 1/d 的重复循环长度:

function recurringCycleLength = findRecurringCycleLength(d)
    remainders = [];
    remainder = 1;
    
    while remainder ~= 0 && isempty(find(remainders == remainder, 1))
        remainders = [remainders, remainder];
        remainder = mod(remainder * 10, d);
    end
    
    if remainder == 0
        recurringCycleLength = 0;
    else
        recurringCycleLength = length(remainders) - find(remainders == remainder, 1) + 1;
    end
end

你可以使用这个函数来确定给定分母 d 的 1/d 的重复循环长度。例如,使用以下命令可以找出 1/7 的重复循环长度:

cycleLength = findRecurringCycleLength(7);
disp(['1/7 的重复循环长度为:', num2str(cycleLength)]);

 这将计算并打印出 1/7 的重复循环长度。

例11.Problem 2340. Numbers spiral diagonals (Part 1)

Inspired by Project Euler n°28 et 58.

A n x n spiral matrix is obtained by starting with the number 1 and moving to the right in a clockwise direction.

For exemple with n=5, the spiral matrix is :

                       21 22 23 24 25
                       20  7  8  9 10
                       19  6  1  2 11
                       18  5  4  3 12
                       17 16 15 14 13

In this example, the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in any n by n spiral (n always odd) ?

HINTS: You want the diagonals, not the whole matrix.

问题2340. 数字螺旋对角线(第1部分)

灵感来自欧拉项目第28和第58题。

通过从数字1开始顺时针向右移动,可以得到一个n x n的螺旋矩阵。

例如,当n=5时,螺旋矩阵如下:

                   21 22 23 24 25
                   20  7   8  9 10
                   19  6  1  2 11
                   18  5  4  3 12
                   17 16 15 14 13

在这个例子中,对角线上数字的总和为101。

对于任意n x n的螺旋(n始终为奇数),对角线上数字的总和是多少?

提示:您需要关注对角线上的数字,而不是整个矩阵。

这是一个经典的数学问题,可以使用 MATLAB 来解决。以下是一个 MATLAB 函数,用于计算任意 n x n 螺旋矩阵对角线上数字的总和:

function diagonalSum = spiralDiagonalSum(n)
    if mod(n, 2) == 0
        error('n必须为奇数');
    end
    
    total = 1;
    current = 1;
    for i = 3:2:n
        for j = 1:4
            current = current + i - 1;
            total = total + current;
        end
    end
    diagonalSum = total;
end

你可以使用这个函数来计算任意 n x n 螺旋矩阵对角线上数字的总和。例如,使用以下命令可以找出 n=5 时的对角线数字总和:

diagonalSum = spiralDiagonalSum(5);
disp(['n=5 时的对角线数字总和为:', num2str(diagonalSum)]);

 这将计算并打印出 n=5 时螺旋矩阵对角线上数字的总和。

例12.Problem 2342. Numbers spiral diagonals (Part 2) 

Inspired by Project Euler n°28 and 58.

A n x n spiral matrix is obtained by starting with the number 1 and moving to the right in a clockwise direction.

For example with n=5, the spiral matrix is :

21 22 23 24 25

20 7 8 9 10

19 6 1 2 11

18 5 4 3 12

17 16 15 14 13

The sum of the numbers on the diagonals is 101 (See problem 2340) and you have 5 primes (3, 5, 7, 13, 17) out of the 9 numbers lying along both diagonals. So the prime ratio is 5/9 ≈ 55%.

With a 7x7 spiral matrix, the ratio is 62% (8 primes out of the 13 diagonal numbers).

What is the side length (always odd and greater than 1) of the square spiral for which the ratio of primes along both diagonals FIRST falls below p% ? (0<p<1)

这里是一个MATLAB函数,可以用来解决你提到的问题2342:

function sideLength = primeRatioSpiral(p)
    n = 3;
    primeCount = 0;
    totalCount = 1;
    while true
        for i = 1:3
            if isprime(n^2 - i*(n-1))
                primeCount = primeCount + 1;
            end
        end
        totalCount = totalCount + 4;
        primeCount = primeCount + sum(isprime((n^2)-[1,3,7,9]*(n-1)));
        if primeCount/totalCount < p
            sideLength = n;
            return
        end
        n = n + 2;
    end
end

 你可以使用这个函数来找出对角线上的素数比率首次低于给定的 p% 时的边长。例如,使用以下命令可以找出当素数比率首次低于20%时的边长:

sideLength = primeRatioSpiral(0.2);
disp(['素数比率首次低于20%时的边长为:', num2str(sideLength)]);

sideLength = primeRatioSpiral(0.2); disp(['素数比率首次低于20%时的边长为:', num2str(sideLength)]);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值