help! writing afunction for isprime

조회 수: 2 (최근 30일)
mustafa aladhami
mustafa aladhami 2016년 9월 23일
댓글: George 2016년 9월 24일
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
George 2016년 9월 24일
Do you know how to test whether a number is prime? Here is one way you can start:
  1. Generate numbers between 2:(n/2)
  2. Test if n is divisible by any of those numbers (eg mod())
  3. Return appropriately based on #2.
  댓글 수: 2
Walter Roberson
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
George 2016년 9월 24일
That's true.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by