Open in App
Not now

Find prime groups after dividing the range 1 to N into K equal sections

• Last Updated : 04 Jan, 2023

Given a positive integer N and an integer K, The task is to find all prime numbers in the range of 1 to N that can be divided into K equal sections

Example:

Input: N = 20, K = 4
Output: 2, 3, 5, 7, 11, 13, 17, 19

Input: N = 30, K = 5
Output: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29

Approach: Sieve of Eratosthenes

The Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit. It works by iterating over all numbers from 2 up to a given number N and marking all its multiples as composite (not prime).

Follow the below steps for the above approach:

• Create a vector sieve of size N+1 and mark it as true to store all prime numbers.
• Then, set number 0 and 1 as not prime number.
• Start marking all multiples of members as non-prime.
• Take the next number in the list that is not marked as composite and mark its multiples in the list as composite.
• Create a vector to store all prime numbers in a range of 1 to N.
• Then, Divide the prime numbers into k equal sections.
• Add all prime numbers in each sections.

Below is the implementation of the above approach:

C++

 `#include ` `#include `   `using` `namespace` `std;`   `// Function to find prime numbers in` `// the range of 1 to N and divide them` `// into K equal sections` `vector<``int``> primeNumberDivision(``int` `N, ``int` `K)` `{` `    ``// sieve[] is used to mark` `    ``// prime numbers in range` `    ``vector<``bool``> sieve(N + 1, ``true``);`   `    ``// set 0 and 1 as not prime` `    ``sieve[0] = sieve[1] = ``false``;`   `    ``// mark all multiples of numbers` `    ``// as non-prime` `    ``for` `(``int` `i = 2; i * i <= N; i++) {` `        ``if` `(sieve[i] == ``true``) {` `            ``for` `(``int` `j = i * i; j <= N; j += i) {` `                ``sieve[j] = ``false``;` `            ``}` `        ``}` `    ``}`   `    ``// vector to store all` `    ``// prime numbers in range` `    ``vector<``int``> prime;`   `    ``// find all prime numbers` `    ``// in the range of 1 to N` `    ``for` `(``int` `i = 2; i <= N; i++) {` `        ``if` `(sieve[i] == ``true``) {` `            ``prime.push_back(i);` `        ``}` `    ``}`   `    ``// divide the prime numbers` `    ``// into K equal sections` `    ``vector<``int``> ans;` `    ``int` `size = prime.size();`   `    ``// check if it's possible` `    ``// to divide into K sections` `    ``if` `(size % K != 0) {` `        ``return` `ans;` `    ``}`   `    ``int` `step = size / K;` `    ``int` `start = 0;` `    ``int` `end = step;`   `    ``// add all prime numbers` `    ``// in each section` `    ``while` `(start < size) {` `        ``for` `(``int` `i = start; i < end; i++) {` `            ``ans.push_back(prime[i]);` `        ``}` `        ``start = end;` `        ``end += step;` `    ``}`   `    ``return` `ans;` `}`   `void` `printAns(vector<``int``>& ans)` `{` `    ``for` `(``auto` `a : ans) {` `        ``cout << a << ``" "``;` `    ``}` `    ``cout << endl;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `N = 20;` `    ``int` `K = 4;` `    ``vector<``int``> ans = primeNumberDivision(N, K);` `    ``printAns(ans);` `    ``return` `0;` `}`

Java

 `// Java implementation` `import` `java.io.*;` `import` `java.util.*;`   `class` `GFG {`   `  ``// Function to find prime numbers in` `  ``// the range of 1 to N and divide them` `  ``// into K equal sections` `  ``static` `List primeNumberDivision(``int` `N, ``int` `K)` `  ``{` `    ``// sieve[] is used to mark` `    ``// prime numbers in range` `    ``boolean``[] sieve = ``new` `boolean``[N + ``1``];` `    ``for` `(``int` `i = ``0``; i < sieve.length; i++) {` `      ``sieve[i] = ``true``;` `    ``}`   `    ``// set 0 and 1 as not prime` `    ``sieve[``0``] = sieve[``1``] = ``false``;`   `    ``// mark all multiples of numbers` `    ``// as non-prime` `    ``for` `(``int` `i = ``2``; i * i <= N; i++) {` `      ``if` `(sieve[i]) {` `        ``for` `(``int` `j = i * i; j <= N; j += i) {` `          ``sieve[j] = ``false``;` `        ``}` `      ``}` `    ``}`   `    ``// list to store all` `    ``// prime numbers in range` `    ``List prime = ``new` `ArrayList<>();`   `    ``// find all prime numbers` `    ``// in the range of 1 to N` `    ``for` `(``int` `i = ``2``; i <= N; i++) {` `      ``if` `(sieve[i]) {` `        ``prime.add(i);` `      ``}` `    ``}`   `    ``// divide the prime numbers` `    ``// into K equal sections` `    ``List ans = ``new` `ArrayList<>();` `    ``int` `size = prime.size();`   `    ``// check if it's possible` `    ``// to divide into K sections` `    ``if` `(size % K != ``0``) {` `      ``return` `ans;` `    ``}`   `    ``int` `step = size / K;` `    ``int` `start = ``0``;` `    ``int` `end = step;`   `    ``// add all prime numbers` `    ``// in each section` `    ``while` `(start < size) {` `      ``for` `(``int` `i = start; i < end; i++) {` `        ``ans.add(prime.get(i));` `      ``}` `      ``start = end;` `      ``end += step;` `    ``}`   `    ``return` `ans;` `  ``}`   `  ``static` `void` `printAns(List ans)` `  ``{` `    ``for` `(``int` `a : ans) {` `      ``System.out.print(a + ``" "``);` `    ``}` `    ``System.out.println();` `  ``}`   `  ``public` `static` `void` `main(String[] args)` `  ``{` `    ``int` `N = ``20``;` `    ``int` `K = ``4``;` `    ``List ans = primeNumberDivision(N, K);` `    ``printAns(ans);` `  ``}` `}`   `// This code is contributed by lokeshmvs21.`

Python3

 `# Python implementation` `from` `typing ``import` `List`   `def` `primeNumberDivision(N: ``int``, K: ``int``) ``-``> ``List``[``int``]:` `  `  `    ``# sieve[] is used to mark prime numbers in range` `    ``sieve ``=` `[``True``] ``*` `(N ``+` `1``)`   `    ``# set 0 and 1 as not prime` `    ``sieve[``0``] ``=` `sieve[``1``] ``=` `False`   `    ``# mark all multiples of numbers as non-prime` `    ``for` `i ``in` `range``(``2``, ``int``(N ``*``*` `0.5``) ``+` `1``):` `        ``if` `sieve[i] ``=``=` `True``:` `            ``for` `j ``in` `range``(i ``*` `i, N ``+` `1``, i):` `                ``sieve[j] ``=` `False`   `    ``# vector to store all prime numbers in range` `    ``prime ``=` `[]`   `    ``# find all prime numbers in the range of 1 to N` `    ``for` `i ``in` `range``(``2``, N ``+` `1``):` `        ``if` `sieve[i] ``=``=` `True``:` `            ``prime.append(i)`   `    ``# divide the prime numbers into K equal sections` `    ``ans ``=` `[]` `    ``size ``=` `len``(prime)`   `    ``# check if it's possible to divide into K sections` `    ``if` `size ``%` `K !``=` `0``:` `        ``return` `ans`   `    ``step ``=` `size ``/``/` `K` `    ``start ``=` `0` `    ``end ``=` `step`   `    ``# add all prime numbers in each section` `    ``while` `start < size:` `        ``for` `i ``in` `range``(start, end):` `            ``ans.append(prime[i])` `        ``start ``=` `end` `        ``end ``+``=` `step`   `    ``return` `ans`   `def` `printAns(ans: ``List``[``int``]) ``-``> ``None``:` `    ``for` `a ``in` `ans:` `        ``print``(a, end``=``' '``)` `    ``print``()`   `# Driver Code` `def` `main():` `    ``N ``=` `20` `    ``K ``=` `4` `    ``ans ``=` `primeNumberDivision(N, K)` `    ``printAns(ans)`   `if` `__name__ ``=``=` `'__main__'``:` `    ``main()`   `# This code is contributed by ksam24000`

C#

 `// C# code to implement the above approach`   `using` `System;` `using` `System.Collections.Generic;`   `public` `class` `GFG {`   `  ``// Function to find prime numbers in` `  ``// the range of 1 to N and divide them` `  ``// into K equal sections` `  ``static` `List<``int``> primeNumberDivision(``int` `N, ``int` `K)` `  ``{` `    ``// sieve[] is used to mark` `    ``// prime numbers in range` `    ``bool``[] sieve = ``new` `bool``[N + 1];` `    ``for` `(``int` `i = 0; i < sieve.Length; i++) {` `      ``sieve[i] = ``true``;` `    ``}`   `    ``// set 0 and 1 as not prime` `    ``sieve[0] = sieve[1] = ``false``;`   `    ``// mark all multiples of numbers` `    ``// as non-prime` `    ``for` `(``int` `i = 2; i * i <= N; i++) {` `      ``if` `(sieve[i]) {` `        ``for` `(``int` `j = i * i; j <= N; j += i) {` `          ``sieve[j] = ``false``;` `        ``}` `      ``}` `    ``}`   `    ``// list to store all` `    ``// prime numbers in range` `    ``List<``int``> prime = ``new` `List<``int``>();`   `    ``// find all prime numbers` `    ``// in the range of 1 to N` `    ``for` `(``int` `i = 2; i <= N; i++) {` `      ``if` `(sieve[i]) {` `        ``prime.Add(i);` `      ``}` `    ``}`   `    ``// divide the prime numbers` `    ``// into K equal sections` `    ``List<``int``> ans = ``new` `List<``int``>();` `    ``int` `size = prime.Count;`   `    ``// check if it's possible` `    ``// to divide into K sections` `    ``if` `(size % K != 0) {` `      ``return` `ans;` `    ``}`   `    ``int` `step = size / K;` `    ``int` `start = 0;` `    ``int` `end = step;`   `    ``// add all prime numbers` `    ``// in each section` `    ``while` `(start < size) {` `      ``for` `(``int` `i = start; i < end; i++) {` `        ``ans.Add(prime[i]);` `      ``}` `      ``start = end;` `      ``end += step;` `    ``}`   `    ``return` `ans;` `  ``}`   `  ``static` `void` `printAns(List<``int``> ans)` `  ``{` `    ``foreach``(``int` `a ``in` `ans) { Console.Write(a + ``" "``); }` `    ``Console.WriteLine();` `  ``}`   `  ``static` `public` `void` `Main()` `  ``{`   `    ``// Code` `    ``int` `N = 20;` `    ``int` `K = 4;` `    ``List<``int``> ans = primeNumberDivision(N, K);` `    ``printAns(ans);` `  ``}` `}`   `// This code is contributed by lokesh.`

Javascript

 `  ``// JS code to implement the approach`   `  ``// Function to find prime numbers in` `  ``// the range of 1 to N and divide them` `  ``// into K equal sections` `  ``function` `primeNumberDivision(N, K) {` `    ``// sieve[] is used to mark` `    ``// prime numbers in range` `    ``let sieve = ``new` `Array(N + 1).fill(``true``);`   `    ``// set 0 and 1 as not prime` `    ``sieve[0] = sieve[1] = ``false``;`   `    ``// mark all multiples of numbers` `    ``// as non-prime` `    ``for` `(let i = 2; i * i <= N; i++) {` `      ``if` `(sieve[i] == ``true``) {` `        ``for` `(let j = i * i; j <= N; j += i) {` `          ``sieve[j] = ``false``;` `        ``}` `      ``}` `    ``}`   `    ``// vector to store all` `    ``// prime numbers in range` `    ``let prime = [];`   `    ``// find all prime numbers` `    ``// in the range of 1 to N` `    ``for` `(let i = 2; i <= N; i++) {` `      ``if` `(sieve[i] == ``true``) {` `        ``prime.push(i);` `      ``}` `    ``}`   `    ``// divide the prime numbers` `    ``// leto K equal sections` `    ``let ans = [];` `    ``let size = prime.length;`   `    ``// check if it's possible` `    ``// to divide leto K sections` `    ``if` `(size % K != 0) {` `      ``return` `ans;` `    ``}`   `    ``let step = size / K;` `    ``let start = 0;` `    ``let end = step;`   `    ``// add all prime numbers` `    ``// in each section` `    ``while` `(start < size) {` `      ``for` `(let i = start; i < end; i++) {` `        ``ans.push(prime[i]);` `      ``}` `      ``start = end;` `      ``end += step;` `    ``}`   `    ``return` `ans;` `  ``}`   `function` `printAns(ans)` `  ``{` `    ``for` `(let a of ans) {` `      ``console.log(a + ``" "``);` `    ``}` `    ``console.log(``"
"``)` `  ``}`   `  ``// Driver Code` `  ``let N = 20;` `  ``let K = 4;` `  ``let ans = primeNumberDivision(N, K);` `  ``printAns(ans);`     `// This code is contributed by Potta Lokesh`

Output

`2 3 5 7 11 13 17 19 `

Complexity Analysis:
Time Complexity: N*log(log(N))
Space Complexity: O(N).

My Personal Notes arrow_drop_up
Related Articles