Generate N integers satisfying the given conditions

• Difficulty Level : Medium
• Last Updated : 04 May, 2021

Given an integer N, the task is to generate an array of size N with the following properties:

1. No two elements divide each other.
2. Every odd subset has an odd sum and every even subset has an even sum.

Examples:

Input: N = 3
Output: 3 5 7
No two element divide each other and the sum
of all the odd subsets {3}, {5}, {7} and {3, 5, 7} is odd.
Sum of all the even subsets is even i.e. {3, 5}, {3, 7} and {5, 7}
Input: N = 6
Output: 3 5 7 11 13 17

Approach: In order to satisfy the condition when every odd subset has an odd sum and even a subset has an even sum, every element has to be odd and in order for any two elements to not divide each other they must be prime. So, the task now is to find the first N odd prime numbers.
Below is the implementation of the above approach:

C++

 // C++ implementation of the approach#include using namespace std; #define MAX 1000000 // Create a boolean array "prime[0..n]" and initialize// all entries it as true. A value in prime[i] will// finally be false if i is Not a prime, else true.bool prime[MAX + 1];void SieveOfEratosthenes(){    memset(prime, true, sizeof(prime));     prime = false;     for (int p = 2; p * p <= MAX; p++) {         // If prime[p] is not changed, then it is a prime        if (prime[p] == true) {             // Set all multiples of p to non-prime            for (int i = p * 2; i <= MAX; i += p)                prime[i] = false;        }    }} // Function to find the first// n odd prime numbersvoid solve(int n){    // To store the current count    // of prime numbers    int count = 0;     // Starting with 3 as 2 is    // an even prime number    for (int i = 3; count < n; i++) {         // If i is prime        if (prime[i]) {             // Print i and increment count            cout << i << " ";            count++;        }    }} // Driver codeint main(){    // Create the sieve    SieveOfEratosthenes();     int n = 6;    solve(n);     return 0;}

Java

 // Java implementation of the approachimport java.util.*; class GFG{static int MAX = 1000000; // Create a boolean array "prime[0..n]" and// initialize all entries it as true.// A value in prime[i] will finally be false// if i is Not a prime, else true.static boolean []prime = new boolean[MAX + 1];static void SieveOfEratosthenes(){    for (int i = 0; i <= MAX; i ++)        prime[i] = true;     prime = false;     for (int p = 2; p * p <= MAX; p++)    {         // If prime[p] is not changed,        // then it is a prime        if (prime[p] == true)        {             // Set all multiples of p to non-prime            for (int i = p * 2; i <= MAX; i += p)                prime[i] = false;        }    }} // Function to find the first// n odd prime numbersstatic void solve(int n){    // To store the current count    // of prime numbers    int count = 0;     // Starting with 3 as 2 is    // an even prime number    for (int i = 3; count < n; i++)    {         // If i is prime        if (prime[i])        {             // Print i and increment count            System.out.print(i + " ");            count++;        }    }} // Driver codepublic static void main(String[] args){    // Create the sieve    SieveOfEratosthenes();     int n = 6;    solve(n);}} // This code is contributed by Rajput-Ji

Python3

 # Python3 implementation of the approachfrom math import sqrt MAX = 1000000 # Create a boolean array "prime[0..n]" and# initialize all entries it as true.# A value in prime[i] will finally be false# if i is Not a prime, else true.prime = [True] * (MAX + 1); def SieveOfEratosthenes() :     prime = False;     for p in range(2, int(sqrt(MAX)) + 1) :         # If prime[p] is not changed,        # then it is a prime        if (prime[p] == True) :             # Set all multiples of p to non-prime            for i in range(p * 2, MAX + 1, p) :                prime[i] = False; # Function to find the first# n odd prime numbersdef solve(n) :     # To store the current count    # of prime numbers    count = 0;    i = 3;     # Starting with 3 as 2 is    # an even prime number    while count < n :         # If i is prime        if (prime[i]) :             # Print i and increment count            print(i, end = " ");            count += 1;                 i += 1 # Driver codeif __name__ == "__main__" :     # Create the sieve    SieveOfEratosthenes();     n = 6;    solve(n); # This code is contributed by AnkitRai01

C#

 // C# implementation of the above approachusing System;     class GFG{static int MAX = 1000000; // Create a boolean array "prime[0..n]" and// initialize all entries it as true.// A value in prime[i] will finally be false// if i is Not a prime, else true.static bool []prime = new bool[MAX + 1];static void SieveOfEratosthenes(){    for (int i = 0; i <= MAX; i ++)        prime[i] = true;     prime = false;     for (int p = 2; p * p <= MAX; p++)    {         // If prime[p] is not changed,        // then it is a prime        if (prime[p] == true)        {             // Set all multiples of p to non-prime            for (int i = p * 2; i <= MAX; i += p)                prime[i] = false;        }    }} // Function to find the first// n odd prime numbersstatic void solve(int n){    // To store the current count    // of prime numbers    int count = 0;     // Starting with 3 as 2 is    // an even prime number    for (int i = 3; count < n; i++)    {         // If i is prime        if (prime[i])        {             // Print i and increment count            Console.Write(i + " ");            count++;        }    }} // Driver codepublic static void Main(String[] args){    // Create the sieve    SieveOfEratosthenes();     int n = 6;    solve(n);}} // This code is contributed by 29AjayKumar

Javascript


Output:
3 5 7 11 13 17

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.5809718,"math_prob":0.9950021,"size":6115,"snap":"2022-05-2022-21","text_gpt3_token_len":2035,"char_repetition_ratio":0.13042055,"word_repetition_ratio":0.52592033,"special_character_ratio":0.3721995,"punctuation_ratio":0.130962,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9993679,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-23T16:17:19Z\",\"WARC-Record-ID\":\"<urn:uuid:b1217b46-2ddd-406a-8a18-f32585c20d7e>\",\"Content-Length\":\"164820\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:38b825da-b709-482d-8bba-76efc4ebd898>\",\"WARC-Concurrent-To\":\"<urn:uuid:b94225b0-074b-4afc-8bc6-cc46b4ef7b0d>\",\"WARC-IP-Address\":\"23.46.150.43\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/generate-n-integers-satisfying-the-given-conditions/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:ITZLC42D6FD2JYAWE5ECFZ5OKFLTDZON\",\"WARC-Block-Digest\":\"sha1:53MFKPRHLNW47SW6AVS5VMZAQWNWPFWV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320304287.0_warc_CC-MAIN-20220123141754-20220123171754-00382.warc.gz\"}"}