New Self New Life
No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
New Self New Life
No Result
View All Result
Home Softwares

Generate and print first N prime numbers

by admin
2 years ago
in Softwares
Best Coding Practices For Rest API Design
Share on FacebookShare on Twitter


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Given a quantity N, the duty is to print the primary N prime numbers.

Examples: 

Enter: N = 4
Output: 2, 3, 5, 7

Enter: N = 1
Output: 2

Strategy: The issue might be solved based mostly on the next concept:

Begin iterating from i = 2, until N prime numbers are discovered. For every i examine if it’s a prime or not and replace the depend of primes discovered until now.

Observe the steps talked about beneath to implement the thought:

  • Create a counter variable (say X = 0) to maintain depend of primes discovered until now and an iterator (say i) to iterate by means of the optimistic integers ranging from 2.
  • Iterate until X turns into N:
    • Examine if i is a major or not.
    • If it’s a prime, print i and improve the worth of X, in any other case, maintain X unchanged.
    • Increment the worth of i by 1.

Beneath is the implementation of the above concept:

C++

// C++ code to implement the method

#embody <bits/stdc++.h>
utilizing namespace std;

// Perform to generate first n primes
void generatePrime(int n)
{
    int X = 0, i = 2;
    bool flag;
    whereas(X < n){
        flag = true;
        for(int j = 2; j <= sqrt(i); j++){
            if (ipercentj == 0){
                flag = false;
                break;
            }
        }
        if(flag){
            cout << i << " ";
            X++;
        }
        i++;
    }
    cout << endl;
}

// Driver code
int primary()
{
    // Check Case 1
    int N = 4;

    // Perform name
    generatePrime(N);
  
    // Check Case 2
    N = 1;
  
    // Perform name
    generatePrime(N);
  
    return 0;
}

Time Complexity: O(X * log X) the place X is the most important prime
Auxiliary House: O(1)



Source link

Tags: GenerateNumbersPrimePrint
Previous Post

57 Best Marketing Books to Read in 2023. Which Have You Read?

Next Post

Zach Braff Calls Ex Florence Pugh a ‘Legend’ in Sweet Birthday Message After Their Split

Related Posts

How to Add Custom Style Variations to WordPress Blocks — Speckyboy
Softwares

How to Add Custom Style Variations to WordPress Blocks — Speckyboy

by admin
June 2, 2025
Smart software replaces expensive sensors for glass wall detection with 96% accuracy
Softwares

Smart software replaces expensive sensors for glass wall detection with 96% accuracy

by admin
June 1, 2025
User Guide For UnoPim PDF Generator
Softwares

User Guide For UnoPim PDF Generator

by admin
May 31, 2025
Infragistics Ultimate 25.1 includes updates across several of its UI toolkit components
Softwares

Infragistics Ultimate 25.1 includes updates across several of its UI toolkit components

by admin
May 29, 2025
Qt bridges the language barrier gap
Softwares

Qt bridges the language barrier gap

by admin
May 28, 2025
Next Post
Zach Braff Calls Ex Florence Pugh a ‘Legend’ in Sweet Birthday Message After Their Split

Zach Braff Calls Ex Florence Pugh a 'Legend' in Sweet Birthday Message After Their Split

DJ Khaled Gets Golf Cart Stuck, Still Gives Motivational Speech

DJ Khaled Gets Golf Cart Stuck, Still Gives Motivational Speech

  • Trending
  • Comments
  • Latest
Anant Ambani wedding: Celebs, wealthy elite attend lavish billionaire festivities – National

Anant Ambani wedding: Celebs, wealthy elite attend lavish billionaire festivities – National

March 1, 2024
Product Information Management Trends (PIM)

Product Information Management Trends (PIM)

February 4, 2022
What is Kubernetes: An Overview

An Introduction to Kubernetes | Developer.com

August 11, 2022
10 really good gadgets that cost less than $100 – TechCrunch

10 really good gadgets that cost less than $100 – TechCrunch

December 17, 2021
Unveiling the Future of Trading

Unveiling the Future of Trading

October 18, 2023
Advancement in predicting software vulnerabilities

Advancement in predicting software vulnerabilities

May 21, 2022
Top 8 Buy Now, Pay Later (BNPL) Services For Businesses

Top 8 Buy Now, Pay Later (BNPL) Services For Businesses

October 22, 2023
Most Useful Gadgets in 2021 – Nogentech.org

Most Useful Gadgets in 2021 – Nogentech.org

July 29, 2021
Samsung may incorporate Perplexity’s AI tech in its phones

Samsung may incorporate Perplexity’s AI tech in its phones

June 2, 2025
Sammy Hagar and Eddie Van Halen Wanted to Write Song on Cello

Sammy Hagar and Eddie Van Halen Wanted to Write Song on Cello

June 2, 2025
Lady Isabella Hervey on bouncing back from her unhappy marriage and her new life on the Algarve

Lady Isabella Hervey on bouncing back from her unhappy marriage and her new life on the Algarve

June 2, 2025
19 Sustainable Clothing Brands For Stylish Men In 2025

19 Sustainable Clothing Brands For Stylish Men In 2025

June 2, 2025
How to Add Custom Style Variations to WordPress Blocks — Speckyboy

How to Add Custom Style Variations to WordPress Blocks — Speckyboy

June 2, 2025
Instagram Creators With Over 100K Followers Will Get Access to Additional Comment Filters

Instagram Creators With Over 100K Followers Will Get Access to Additional Comment Filters

June 2, 2025
Fantastic Four Star Vanessa Kirby Is Pregnant!

Fantastic Four Star Vanessa Kirby Is Pregnant!

June 2, 2025
Inside Zendaya and Tom Holland’s Marvelous Love Story

Inside Zendaya and Tom Holland’s Marvelous Love Story

June 1, 2025
New Self New Life

Your source for entertainment news, celebrities, celebrity news, and Music, Cinema, Digital Lifestyle and Social Media and More !

Categories

  • Celebrity
  • Cinema
  • Devices
  • Digital Lifestyle
  • Entertainment
  • Music
  • Social Media
  • Softwares
  • Uncategorized

Recent Posts

  • Samsung may incorporate Perplexity’s AI tech in its phones
  • Sammy Hagar and Eddie Van Halen Wanted to Write Song on Cello
  • Lady Isabella Hervey on bouncing back from her unhappy marriage and her new life on the Algarve
  • Home
  • Disclaimer
  • DMCA
  • Privacy Policy
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites. slotsfree  creator solana token

No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites.

slot machine bonus