The Wayback Machine - https://web.archive.org/web/20240809062337/https://www.geeksforgeeks.org/number-unmarked-integers-special-sieve/
Open In App

Number of unmarked integers in a special sieve

Last Updated : 16 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an array A containing numbers from 2 to N.. A special type of sieving is done on it . 
The procedure of sieving is as follows: 

  1. Create an array with elements as consecutive integers from 2 to N and mark every element in the array as unmarked.
  2. Let an integer Q = N and mark all the proper divisors of Q except Q itself in the array.
  3. Find the largest number unmarked less than Q and assign Q to it, and repeat from step 2. If there are no more unmarked elements then stop.

Find the number of unmarked integers after sieving.

Examples:  

Input : N = 5 
Output : Number of unmarked elements = 3
Explanation : We create array A[] = { 2, 3, 4, 5 }.
2 is marked as it is a proper divisor of 4.

Input : N = 4
Output : Number of unmarked elements = 2

Naive Approach: 

One basic approach is to run two loops. Outer loop to traverse the whole array and inner loop for traversing from 2 – Q to unmark all the proper divisors of Q by checking a[i] % Q = 0.

Time Complexity: O(N^2)

Efficient Approach:  A simple observation suggests that actually there is no need to do sieving here instead, the value of N will determine the answer. 

  • Case 1: If N is Odd then number of unmarked elements will be (N/2) + 1.
  • Case 2: If N is Even then number of unmarked elements will be (N/2).

Time Complexity: O(1)

Examples: 

Input : N = 5 
Output : 3 
A[] = { 2, 3, 4, 5 } 

Steps: 

  1. Q = 5 : Mark All the proper divisors of Q, here no element is there so every element remains unmarked. 
  2. Q = 4 : Mark all the proper divisors of Q. Here 2 gets marked and unmarked elements are {3, 4, 5}. 
  3. Q = 3 : 
  4. Now no further marking can be done so stop here 

So number of unmarked elements are 3 i.e {3, 4, 5} 
In case of ODD value of N result should be (N/2)+1 = (3/2)+1 = (5/2)+1 = 2+1= 3.

Input : N = 4 
Output : 2 
A[] = { 2, 3, 4 } 

Steps: 

  1. Q = 4 Mark all the proper divisors of Q. So here 2 gets marked and unmarked elements are {3, 4} 
  2. Q = 3 
  3. Now no further marking can be done so stop here 

So number of unmarked element after sieving are {3, 4} = 2 
In case of EVEN value of N result should be (N/2) = (4/2) = 2 

Implementation:

C++




// C++ Program to determine the
// number of unmarked integers in
// a special sieve
#include <bits/stdc++.h>
using namespace std;
 
int countUnmarked(int N)
{
    if (N % 2 == 0)
       return N/2;
    else
       return N/2 + 1;
}
 
// Driver Code
int main()
{
    int N = 4;
    cout << "Number of unmarked elements: "
         << countUnmarked(N) << endl;
    return 0;
}


Java




// Java Program to determine
// the number of unmarked
// integers in a special sieve
import java.io.*;
 
class GFG
{
static int countUnmarked(int N)
{
    if (N % 2 == 0)
    return N / 2;
    else
    return N / 2 + 1;
}
 
// Driver Code
public static void main (String[] args)
{
    int N = 4;
    System.out.println("Number of unmarked " +
                                "elements: " +
                            countUnmarked(N));
}
}
 
// This code is contributed
// by anuj_67.


Python3




# Python3 Program to determine
# the number of unmarked
# integers in a special sieve
def countUnmarked(N):
    if (N % 2 == 0):
        return N / 2;
    else:
        return N / 2 + 1;
 
# Driver Code
N = 4;
print("Number of unmarked elements:",
              int(countUnmarked(N)));
     
# This code is contributed
# by mits


C#




// C# Program to determine
// the number of unmarked
// integers in a special sieve
using System;
 
class GFG
{
static int countUnmarked(int N)
{
    if (N % 2 == 0)
        return N / 2;
    else
        return N / 2 + 1;
}
 
// Driver Code
public static void Main ()
{
    int N = 4;
    Console.WriteLine("Number of unmarked " +
                               "elements: " +
                           countUnmarked(N));
}
}
 
// This code is contributed
// by anuj_67.


PHP




<?php
// PHP Program to determine
// the number of unmarked
// integers in a special sieve
 
function countUnmarked($N)
{
    if ($N % 2 == 0)
    return $N / 2;
    else
    return $N / 2 + 1;
}
 
// Driver Code
$N = 4;
echo "Number of unmarked elements: ",
                   countUnmarked($N);
     
// This code is contributed
// by anuj_67.
?>


Javascript




<script>
// javascript Program to determine
// the number of unmarked
// integers in a special sieve
    function countUnmarked(N) {
        if (N % 2 == 0)
            return N / 2;
        else
            return N / 2 + 1;
    }
 
    // Driver Code
     
        var N = 4;
        document.write("Number of unmarked "
        + "elements: " + countUnmarked(N));
 
// This code is contributed by todaysgaurav
</script>


Output

Number of unmarked elements: 2

Time complexity: O(1)
Auxiliary space: O(1)



Similar Reads

How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
Pre-requisite: Sieve of Eratosthenes What is Sieve of Eratosthenes algorithm? In order to analyze it, let's take a number n and the task is to print the prime numbers less than n. Therefore, by definition of Sieve of Eratosthenes, for every prime number, it has to check the multiples of the prime and mark it as composite. This process continues unt
3 min read
Longest sub-array of Prime Numbers using Segmented Sieve
Given an array arr[] of N integers, the task is to find the longest subarray where all numbers in that subarray are prime. Examples: Input: arr[] = {3, 5, 2, 66, 7, 11, 8} Output: 3 Explanation: Maximum contiguous prime number sequence is {2, 3, 5} Input: arr[] = {1, 2, 11, 32, 8, 9} Output: 2 Explanation: Maximum contiguous prime number sequence i
13 min read
Sum of all Primes in a given range using Sieve of Eratosthenes
Given a range [L, R]. The task is to find the sum of all the prime numbers in the given range from L to R both inclusive. Examples: Input : L = 10, R = 20Output : Sum = 60Prime numbers between [10, 20] are:11, 13, 17, 19Therefore, sum = 11 + 13 + 17 + 19 = 60Input : L = 15, R = 25Output : Sum = 59Recommended: Please try your approach on {IDE} first
12 min read
Segmented Sieve (Print Primes in a Range)
Given a range [low, high], print all primes in this range? For example, if the given range is [10, 20], then output is 11, 13, 17, 19. A Naive approach is to run a loop from low to high and check each number for primeness. A Better Approach is to precalculate primes up to the maximum limit using Sieve of Eratosthenes, then print all prime numbers i
15 min read
Sieve of Atkin
Given a limit, print all prime numbers smaller than or equal to the given limit. Examples: Input: limit = 10Output: 2, 3, 5, 7 Input: limit = 20Output: 2, 3, 5, 7, 11, 13, 17, 19 Other Approaches: The prime numbers till a limit can be generated using other algorithms, such as: Sieve of Eratosthenes: O(N*log(log(N)))Sieve of Sundaram: O(N*log N) Sie
15+ min read
Sieve of Eratosthenes in 0(n) time complexity
The classical Sieve of Eratosthenes algorithm takes O(N log (log N)) time to find all prime numbers less than N. In this article, a modified Sieve is discussed that works in O(N) time.Example : Given a number N, print all prime numbers smaller than N Input : int N = 15 Output : 2 3 5 7 11 13 Input : int N = 20 Output : 2 3 5 7 11 13 17 19 Manipulat
12 min read
Sieve of Eratosthenes
Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.  Example:  Input : n =10Output : 2 3 5 7  Input : n = 20 Output: 2 3 5 7 11 13 17 19 The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million or so. Recommended ProblemTechf
9 min read
Bitwise Sieve
Given a number n, print all primes smaller than n.Examples : Input : 30 Output : 2 3 5 7 11 13 17 19 23 29 Input : n = 100 Output : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 We know how to calculate all primes less than n by the Sieve of Eratosthenes. Below is an implementation of Sieve. One optimization in the below im
15+ min read
Segmented Sieve
Given a number n, print all primes smaller than n. For example, if the given number is 10, output 2, 3, 5, 7. Recommended PracticeProduct of PrimesTry It!A Naive approach is to run a loop from 0 to n-1 and check each number for primeness. A Better Approach is to use Simple Sieve of Eratosthenes. [GFGTABS] C++ #include &lt;iostream&gt; #include &lt;
15+ min read
Prime Factorization using Sieve O(log n) for multiple queries
We can calculate the prime factorization of a number "n" in O(sqrt(n)) as discussed here. But O(sqrt n) method times out when we need to answer multiple queries regarding prime factorization.In this article, we study an efficient method to calculate the prime factorization using O(n) space and O(log n) time complexity with pre-computation allowed.
11 min read
three90RightbarBannerImg