简体   繁体   中英

Prime numbers in array

I need to write a function that recieve from the user a number(n), and the function return an array with all the prime numbers until the user number(n). I know how to write the function that check if number is prime, but i dont know how to enter the numbers to an array.. for example if the user entered 53, it will return [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53]. I forgot to tell that the language is java.. my bad!

Here is the code

private ArrayList getPrimeNumbers(int number)
{
    ArrayList primeNumbers = new ArrayList();
    for (int i = 0; i <= number; i++)
    {
        if(isPrime(i)) 
        {
            primeNumbers.add(i);
        }
    }
    return primeNumbers;
} 


private boolean isPrime(int n) 
{
    for(int i=2;i<n;i++) 
    {
        if(n%i==0)
            return false;
    }
    return true;
}

Usually, you can't change array length , so you should use the appropriate collection where you can add items; that is List<> in C#, ArrayList<> in Java etc.

Technically, you can implement separate IsPrime method and then check the integers in 2..max range, but here it can just be inlined.

Possible code in C#

public static int[] GetPrimes(int max) {
  if (max <= 1)
    return new int[0];
  else if (max == 2) // <- let's process the only even prime (2) separately
    return new int[] { 2 };

  List<int> primes = new List<int>() { 2, 3 };

  // Sieve through odd numbers:    
  for (int i = 5; i <= max; i += 2) {
    int sqrt = (int) Math.Sqrt(i + 1);

    Boolean isPrime = true;

    // There's no need to check if (i % primes[0] == 0):
    // primes[0] == 2, and loop is for odd numbers - 5, 7, 9,...
    for (int j = 1; primes[j] <= sqrt; ++j)
      if ((i % primes[j]) == 0) {
        isPrime = false;
        break;
      }

    if (isPrime)
      primes.Add(i);
  }

  return primes.ToArray();
}
// ....
// Test will return the string
// "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53"
String result = String.Join(", ", GetPrimes(53).Select(x => x.ToString()));

Here is one way to do it (you did not specify language, so I'm using Python):

def GetPrimes(n):
    array = []
    for i in range(2,n+1):
        if IsPrime(i):
            array.append(i)
    return array

def IsPrime(n):
    for i in range(2,n/2+1):
        if n % i == 0:
            return False
    return True

print GetPrimes(53)

The following code will work, Give it a try package CoreJava; public class prime {

public static void main(String[] args) {        
    int a [] ={1, 2, 3, 7, 23,7,10};
    int flag=0;
    for(int i=0;i<a.length-1;i++)
    {
    if((a[i]!=0) &&(a[i]!=1))
    {
    for(int j=2;j<a[i];j++)
    {
      if(a[i]%j==0)
    {
       flag=1;
       break;
    }
    }
    if(flag==0)
    {
     System.out.println(a[i]);
    }
    }
    
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM