Prime function
6 views (last 30 days)
Show older comments
[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.
0 Comments
Answers (1)
Walter Roberson
on 13 Nov 2011
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!