matlab loops break and continue commands

In number theory, a semi-perfect number is a positive integer number that is equal to the sum of its largest three positive divisors, excluding the number itself. For example; 18 is a semiperfect number, the positive divisors of this number are 1,2,3,6,9,18, and the sum of the three largest integer divisors (excluding the number itself) is 3 + 6 + 9 = 18. Write a MATLAB program that checks whether a given number is semi-perfect number.

2 件のコメント

Ameer Hamza
Ameer Hamza 2020 年 4 月 9 日
This seems like a homework question. What have you already tried?
mehmet kiraccakali
mehmet kiraccakali 2020 年 4 月 9 日
編集済み: mehmet kiraccakali 2020 年 4 月 9 日
n=input('Enter a number ');
sum=0;
for i=n-1:-1:1
if mod(n,i)==0
sum=sum+i;
end
end
if sum==n
fprintf('%d is a semi-perfect number \n',n);
else
fprintf('%d is not a semi-perfect number \n',n);
end
I couldn't solve this question either, I tried to make the solution above. but i don't know how to find the three biggest divisors.

サインインしてコメントする。

回答 (1 件)

Peng Li
Peng Li 2020 年 4 月 9 日

0 投票

You are almost there. Just need a seperate counter that force the loop to break if it accumulates to 3.
n = input('Enter a number ');
sum = 0;
ind = 0;
for i = n-1:-1:1
if mod(n, i) == 0
sum = sum + i;
ind = ind + 1;
if ind == 3
break;
end
end
end
if sum == n
fprintf('%d is a semi-perfect number \n',n);
else
fprintf('%d is not a semi-perfect number \n',n);
end

7 件のコメント

mehmet kiraccakali
mehmet kiraccakali 2020 年 4 月 9 日
thank you so much
Ayberk Dönmez
Ayberk Dönmez 2020 年 4 月 9 日
write a matlab program that calculates the sum of the largest positive divisor (excluding itself) and the smallest positive divisor (excluding 1) of a given number? do you have any idea about this question ?
Peng Li
Peng Li 2020 年 4 月 9 日
You can keep doing your way of using a loop; store all divisors found; and do a min/max after that.
A better work around:
n = input('Enter a number ');
k = 2:ceil(n/2); % you don't need to search through bigger numbers, as they won't be a divisor anyway
d = k(rem(n, k) == 0); % you get all divisors
if isempty(d)
disp('no divisors found except the number itself and 1');
else
yourNeedResult = d(1) + d(end);
fprintf("the largest positve divisor (excluding itself) of %d is %d\r", n, d(end));
fprintf("the smallest positive divisor (excluding 1) of %d is %d\r", n, d(1));
fprintf("there sum is %d\r", d(1)+d(end));
end
Enter a number 18
the largest positve divisor (excluding itself) of 18 is 9
the smallest positive divisor (excluding 1) of 18 is 2
there sum is 11
Walter Roberson
Walter Roberson 2020 年 4 月 9 日
Hint: mod(number, potential_factor)
Gamze Elmastas
Gamze Elmastas 2020 年 4 月 9 日
What is the mean of if ind==3? I dont know the mean of ind.
Ayberk Dönmez
Ayberk Dönmez 2020 年 4 月 9 日
thank you so much
Walter Roberson
Walter Roberson 2020 年 4 月 9 日
sum of its largest three positive divisors
That is why 3 was chosen to compare against.
ind is being used to count how many factors have been found so far.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2020 年 4 月 9 日

コメント済み:

2020 年 4 月 9 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by