Prime number check (★★) - MATLAB Cody - MATLAB Central

Problem 45767. Prime number check (★★)

One way to see if a number x is prime is to compute the remainders obtained when dividing x by all integers from 2 to √(x). If x is prime, then all remainders will be non-zero.
For example, if x = 35 then √(x)=5.916, and the set of divisors from 2 to √(x) are [2 3 4 5]. The remainders obtained when dividing 35 by 2,3,4,5 are 1,2,3,0 respectively. Since one of the remainders is 0, the number x is not prime.
Write a function that will compute the remainders obtained when dividing x by all integers from 2 to √(x).
Avoid using for/while loops.

Solution Stats

53.4% Correct | 46.6% Incorrect
Last Solution submitted on May 05, 2025

Problem Comments

Solution Comments

Show comments
Why should you share code?
In a discussion on LInkedin about my recent blog post, Do these...
3
6

Problem Recent Solvers49

Suggested Problems

More from this Author52

Problem Tags

Community Treasure Hunt

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

Start Hunting!