Prime function
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
[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 comentarios
Respuestas (1)
Walter Roberson
el 13 de Nov. de 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 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!