Get two input from user, 2 num, and display all the perfect numbers between this two numbers.
Show older comments
To accept two numbers from the user and display perfect numbers between these two numbers. I am trying to create a script with this question. I am stuck at,
clc num1 = input('Enter 1st num: '); num12 = input('Enter 2nd num: '); n = num1:num2;
Answers (1)
Stephen23
on 14 Sep 2015
Try this:
function out = perfect(N1,N2)
P = [2,3,5,7,13,17,19,31,61,89,107,127,521,607]; % Mersenne primes.
C = arrayfun(@(p)repmat('10',p,1),P,'UniformOutput',false);
out = cellfun(@(s)bin2dec(s(1:end-1)),C);
out(N1>out|out>N2) = [];
end
And some examples of it being used:
>> perfect(1,100)
ans =
6 28
>> perfect(8128,1e10)
ans =
8128 33550336 8589869056
Categories
Find more on Mathematics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!