help! writing afunction for isprime
显示 更早的评论
i'm not sure how to write this since i'm new to matlab!
Write a function that receives a number n and determine if it is a prime number (output 1) or not (output 0). You can not use the ‘isprime’ function.
回答(1 个)
George
2016-9-24
0 个投票
Do you know how to test whether a number is prime? Here is one way you can start:
- Generate numbers between 2:(n/2)
- Test if n is divisible by any of those numbers (eg mod())
- Return appropriately based on #2.
2 个评论
Walter Roberson
2016-9-24
You do not need to go as far as n/2, you can stop at sqrt() of the number
George
2016-9-24
That's true.
类别
在 帮助中心 和 File Exchange 中查找有关 Discrete Math 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!