getting prime numbers between 2 numbers input by the user
이전 댓글 표시
num1 = input('Enter Number 1: '); %prompt user for first number
num2 = input('Enter Number 2: '); %prompt user for second number
for x = num1:num2 %x is numbers in between num1 and num2
y = primes(x); %y is to find the primes between the 2 number
end
fprintf('The primes between %d and %d are: \n',num1, num2); %tells the user the output
disp(y); %display the primes
this is my code and when the user input 15 and 30 for the first and second number, instead of getting prime numbers just from 15 to 30, the result is prime numbers from 1 to 30, how should i modify the code to get prime numbers just between num1 and num2? thanks!
채택된 답변
추가 답변 (1개)
Image Analyst
2017년 9월 20일
One approach is to computer primes for both numbers and use setdiff():
num1Primes = primes(num1-1)
num2Primes = primes(num2)
result = setdiff(num2Primes, num1Primes)
카테고리
도움말 센터 및 File Exchange에서 Language Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!