Prime function

6 次查看(过去 30 天)
emrah kara
emrah kara 2011-11-13
[EDIT: 20111113 15:00 CST - merge questions - WDR]
Write a function named function p = isPrime(n) that returns 1 if n is a prime number and returns 0 otherwise.
[Information from second question]
Write a function named function v = primeList(n) that returns list(vector) of prime numbers less than or equal to n.

回答(1 个)

Walter Roberson
Walter Roberson 2011-11-13
Sorry, it is not possible to write either of those functions.
Consider the output required for isPrime(inf): it is undefined as to whether infinity is prime or not, so returning either 0 or 1 would be improper.
Consider the output required for primeList(inf): it has been proven (in numerous ways) that there are an infinite number of primes, so primeList(inf) would have to return an infinite list, which would require an infinite amount of storage and an infinite time.
Consider the output required for
sym x
isPrime(x)
Since the value associated with the symbol is unknown, it is not possible for isPrime to decide whether x is prime or not, so it cannot return either 1 or 0.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by