简体   繁体   English

MATLAB中的Smallest_multiple函数

[英]Smallest_multiple function in MATLAB

Hi I am having problems with the following function in Matlab. 嗨,我在Matlab中使用以下功能遇到问题。 Can some please help? 可以帮忙吗? 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. 2520是可以除以1到10的每个数字而没有任何余数的最小数字。 Write a function called smallest_multiple that returns a uint64, the smallest positive number that is evenly divisible by all of the numbers from 1 to n where n is a positive integer scalar and is the only input argument of the function. 编写一个名为smallest_multiple的函数,该函数返回一个uint64,这是最小的正数,它可以被从1到n的所有数均分,其中n是一个正整数标量,并且是该函数的唯一输入参数。 If the result would be greater than what can be represented as a uint64, the function returns 0. (Inspired by Project Euler.) 如果结果大于可以表示为uint64的结果,则该函数返回0。(受Project Euler启发。)

Below is the code I wrote for the function but it gives error Feedback: Your function made an error for argument(s) 2 下面是我为该函数编写的代码,但它给出了错误反馈:您的函数对参数2进行了错误处理

Your solution is _not_ correct.

Help please... 请帮助...

function [answer]=smallest_multiple(n)
limit = 1e10;

N = 20;

for i = N:N:limit

    for j = N:-1:1

    if mod(i,j) ~= 0

        break

    end

end

if j == 1

    answer = i;

    break

    end

end

fprintf('The smallest evenly divisible number is %.0d\n',answer)

Your function looks correct. 您的功能看起来正确。 However the argument you pass is a lowercase n instead of an uppercase N , which you use during your code. 但是,您传递的参数是小写的n而不是您在代码中使用的大写的N

So the correct function (with limit as argument) is 因此正确的函数(以limit为参数)是

function [answer]=smallest_multiple(N,limit)

for i = N:N:limit

    for j = N:-1:1

        if mod(i,j) ~= 0
            break
        end

    end

    if j == 1
        answer = i;
        break
    end

end

fprintf('The smallest evenly divisible number is %.0d\n',answer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM