Computer Science, asked by jasminejohn522, 8 months ago

Take a range of 0 - 1000 Numbers and find the Prime numbers in that range. Store the prime numbers in a 2D Array, the first dimension represents the range 0-100, 100-200, and so on. While the second dimension represents the prime numbers in that range

Answers

Answered by devc5622
0

Answer:

if its in java then

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class ExArrayPrimeNumberMatrix

{

// Function to check a number is prime or not

boolean isPrime(int n)  

{

 int c = 0;

 for(int i = 1; i<=n; i++)

 {

  if(n%i == 0)

   c++;

 }

 if(c == 2)

  return true;

 else

  return false;

}

public static void main(String args[])throws IOException

{

 // create object of Prime number matrix.

 ExArrayPrimeNumberMatrix ob = new ExArrayPrimeNumberMatrix();

 // create object of buffer stream.

 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

 // enter the number of rows and column.

 System.out.print("Enter the number of rows : ");

 int m=Integer.parseInt(br.readLine());

 System.out.print("Enter the number of columns : ");

 int n=Integer.parseInt(br.readLine());

 // 2D array for storing 'm*n' prime numbers

 int A[][]=new int[m][n];

 // 1D array for storing 'm*n' prime numbers

 int B[] = new int [m*n];

 // For taking natural numbers

 int i = 0, j;

 int k = 1;  

 // for iD Array.

 while(i < m*n)

 {

  if(ob.isPrime(k)==true)

  {

   B[i] = k;

   i++;

  }

  k++;

 }

 // for 2D Array.

 int x = 0;

 for(i=0; i<m; i++)

 {

  for(j=0; j<n; j++)

  {

   A[i][j] = B[x];

   x++;

  }

 }

 // printing the result in 2D Array.

 System.out.println("The Final Array is : ");

 for(i=0; i<m; i++)

 {

  for(j=0; j<n; j++)

  {

   System.out.print(A[i][j]+"\t");

  }

  System.out.println();

 }

}

}

for c++  

// C++ program to print print all primes in a range  

// using concept of Segmented Sieve  

#include <iostream.h>  

// This functions finds all primes smaller than limit  

// using simple sieve of eratosthenes. It stores found  

// primes in vector prime[]  

void simpleSieve(int limit, vector<int>& prime)  

{  

bool mark[limit + 1];  

memset(mark, false, sizeof(mark));  

for (int i = 2; i <= limit; ++i) {  

 if (mark[i] == false) {  

  // If not marked yet, then its a prime  

  prime.push_back(i);  

  for (int j = i; j <= limit; j += i)  

   mark[j] = true;  

 }  

}  

}  

// Finds all prime numbers in given range using  

// segmented sieve  

void primesInRange(int low, int high)  

{  

// Comput all primes smaller or equal to  

// square root of high using simple sieve  

int limit = floor(sqrt(high)) + 1;  

vector<int> prime;  

simpleSieve(limit, prime);  

// Count of elements in given range  

int n = high - low + 1;  

// Declaring boolean only for [low, high]  

bool mark[n + 1];  

memset(mark, false, sizeof(mark));  

// Use the found primes by simpleSieve() to find  

// primes in given range  

for (int i = 0; i < prime.size(); i++) {  

 // Find the minimum number in [low..high] that is  

 // a multiple of prime[i] (divisible by prime[i])  

 int loLim = floor(low / prime[i]) * prime[i];  

 if (loLim < low)  

  loLim += prime[i];  

 if(loLim==prime[i])  

  loLim += prime[i];  

 /* Mark multiples of prime[i] in [low..high]:  

  We are marking j - low for j, i.e. each number  

  in range [low, high] is mapped to [0, high - low]  

  so if range is [50, 100] marking 50 corresponds  

  to marking 0, marking 51 corresponds to 1 and  

  so on. In this way we need to allocate space only  

  for range */

 for (int j = loLim; j <= high; j += prime[i])  

  mark[j - low] = true;  

}  

// Numbers which are not marked in range, are prime  

for (int i = low; i <= high; i++)  

 if (!mark[i - low])  

  cout << i << " ";  

}  

// Driver program to test above function  

int main()  

{  

int low = 10, high = 100;  

primesInRange(low, high);  

return 0;  

}  

Answered by shashankshekhar2311
0

Answer:

public class Prime2DArray {

// method to check number is prime number or not

   public static boolean isPrime(int n) {  

       if (n == 0 || n == 1) {

           return false;

       }

       int i = 2;

       while (i <= n / 2) {

           if (n % i == 0) {

               return false;

           }

           i++;

       }

       return true;

   }

   public static void main(String[] args) {

       int prime[][] = new int[11][2];  //creation of 2d array

       int k = 0;

       int count = 0;  //to count how many prime number is there

       for (int i = 0; i <= 1000; i++) {

           boolean x = isPrime(i);

           if (x) {

               count++;

           }

           if (i % 100 == 0 && i != 0) {

               prime[k][0] = i;

               prime[k][1] = count;

               count = 0;

               k++;

           }

       }

       int j = 0;

       while (j < 2) {

           for (int n = 0; n < 10; n++) {

               System.out.print(prime[n][j] + " ");

           }

           System.out.println();

           j++;

       }

   }

}

Explanation:

Similar questions