How to convert flowchart to MATLAB code ?
17 ビュー (過去 30 日間)
古いコメントを表示
Dear Sirs, please help me to convert this flowchart to MATLAB code.
I am facing some difficulties in loops I tried hard to solve it several times but I couldn't.
it is about inputting a number and printing out if it prime number or not.
I would appreciate it if you could help me with this.
3 件のコメント
回答 (2 件)
DGM
2023 年 11 月 16 日
This is what I was squatting on. It's not a literal interpretation of the flowchart, but there's no good reason to use a literal interpretation.
% don't harrass the user for inputs
% unless you like collecting typos
N = 456156445111;
% use a flag instead of a scattered bunch of redundant disp() calls
% avoid entering the loop if at all possible
P = [2 3 5 7 11 13 17 19 23 29 31 37]; % known small primes (as many as you want)
if ismember(N,[1 P]) % is N a known small prime?
nisprime = true;
elseif any(~mod(N,P)) % does N have known small prime factors?
nisprime = false;
else % this is slow
for C = 2:N-1
if ~mod(N,C)
nisprime = false;
break;
end
end
nisprime = true;
end
if nisprime
disp('prime')
else
disp('not prime')
end
There are plenty of better examples in other threads. I just figured I'd dump this so that I can delete my forum notes.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Programmatic Scenario Authoring についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!