# MATLAB Roots Function 問題

Matlab 問題

Write a MATLAB script that, given a number, finds a given quantity of consecutive integers whose product equals that total. For example, given a product of 24 and a quantity of 3, the script should return 2, 3, and 4, because 2*3*4=24. The script must meet the following criteria:

a) Asks the user for the product

b) Asks the user for the quantity of consecutive integers sought

c) Uses the roots function to find one of the sought integers

Generate output for the following cases:

Case 1: Product=6, Integers=3

Case 2: Product=97290, Integers=3

Case 3: Product= 53721360, Integers=6

(Hint: An equation describing Case 1 above can be written as (x)(x+1)(x+2)-6=0)

### 1 個解答

• 最佳解答

clear

clc

%Problem 3

a=[1 0];

b=[1 1];

c=[1 2];

%Asks the user for product and quantity

integers=input('Integer')

product=input('Product')

%Find the sought integers

%When integers equal 3

if integers==3

First=conv(a,b);

Second=conv(First,c);

%When integers equal 6

elseif integers==6

First=conv(a,b);

Second=conv(First,c);

Third=conv(Second,[1 3]);

Fourth=conv(Third,[1 4]);

Fifth=conv(Fourth,[1 5]);

end

%When product equal 6

if product==6

i=Second-[0 0 0 6];

fprintf('Case 1\n')

%When product equal 97290

elseif product==97290

i=Second-[0 0 0 97290];

fprintf('Case 2\n')

%When product equal 53721360

elseif product==53721360

i=Fifth-[0 0 0 0 0 0 53721360];

fprintf('Case 3 \n')

end

%Roots function

r=roots(i);

fprintf('The one of the sought integers is %.2f \n',r(end))

參考資料： 自己