# How can I make this work? input x = 10^13

1 ビュー (過去 30 日間)
A C 2019 年 4 月 15 日
コメント済み: Walter Roberson 2019 年 4 月 15 日
Given a number x, determine how many factors numbers n are less than or equal to that number. The code does takes a long time to finish when I use 10^6 or above. I have not waited on it. The inputs are x and n. I am using x = 10^13 and n = 3. The outcome will be 624. How can I vectorize it to make it faster?
x = 10^13;
n = 3;
y2 = 1;
for k = 2:x;
% factor 1:n
fact = factor(k);
if all(fact <= n)
y2 = y2 + 1;
end
end

#### 2 件のコメント

Walter Roberson 2019 年 4 月 15 日
Is 8 a factor of 16?
A C 2019 年 4 月 15 日
Yes. Maybe I did not make it understandable.
I want factor(16) >>> return 2 2 2 2 . The built in function serve what I am aiming for.
16 would be an counted.
factor(5) >>> 5 could not be counted if n = 3.

サインイン to comment.

### 回答 (1 件)

Walter Roberson 2019 年 4 月 15 日
You have not documented the purpose of the code, so we cannot know what the desired outcome is.
You use factor(). The Mathworks provided factor() returns something that is a vector unless the input is a prime. You then compare that likely vector to n. Remember that if VECTOR is considered true only if all the entries in the vector are non-zero. Your code therefore ends up testing that all of the factors of the number are <= 5. If that is your intent, then we recommand that you code
if all(fact <= n)
instead of people having to guess about whether you understood how if and while work with vectors.

#### 2 件のコメント

A C 2019 年 4 月 15 日