how do i check if a number is a prime number, a square, and or a factor of 6?
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Vjon Joson
el 1 de Abr. de 2020
here is the current idea for the code so far
integer = 4;
if % ** check if integer is a prime **
Prime = integer % => If condition is met
elseif % ** check if the number is square **
Square = integer % => Else condition is met
elseif % chekc if divisible by 6
Factorof6=integer
end
3 comentarios
Walter Roberson
el 5 de En. de 2023
https://www.mathworks.com/matlabcentral/answers/514501-how-do-i-check-if-a-number-is-a-prime-number-a-square-and-or-a-factor-of-6#comment_819187
DGM
el 5 de En. de 2023
Editada: DGM
el 5 de En. de 2023
Since the core answers are all basically already on this page, I'll just add that logical indexing, implicit casting, and implicit expansion can make this tidy.
x = 100:999; % a set of numbers as a row vector
issquare = ~mod(sqrt(x),1); % implicit binarization
ismultiple = ~mod(x,[2; 3; 5]); % implicit expansion
validnums = issquare & ~any(ismultiple,1); % logical combination of masks
x(validnums) % logical indexing
Now you have to make a function out of it.
Respuesta aceptada
Birdman
el 1 de Abr. de 2020
n=4;
%check if number is prime
isprime(n) %returns logical value
%check if number is square
mod(numel(factor(n)),2)==0 %returns logical value
%check if number is divisible by 6
mod(n,6)==0 %returns logical value
7 comentarios
Walter Roberson
el 16 de Oct. de 2022
https://www.mathworks.com/matlabcentral/answers/514501-how-do-i-check-if-a-number-is-a-prime-number-a-square-and-or-a-factor-of-6#comment_819105 shows a revised test (which I did not examine)
Más respuestas (0)
Ver también
Categorías
Más información sobre Function Creation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!