对象已移动

可在此处找到该文档 Generate and print first N prime numbers – New Self New Life
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

Huawei Cloud rolls out Pangu Models 5.5 to cover more industries
Softwares

Huawei Cloud rolls out Pangu Models 5.5 to cover more industries

by admin
June 24, 2025
Minor update(4) for Vivaldi Android Browser 7.4
Softwares

Minor update(4) for Vivaldi Android Browser 7.4

by admin
June 21, 2025
How AI Medical Coding Software Reduces Errors & Accelerates Billing in 2025
Softwares

How AI Medical Coding Software Reduces Errors & Accelerates Billing in 2025

by admin
June 22, 2025
10+ Best Free Portfolio & Lookbook Templates for InDesign in 2025 — Speckyboy
Softwares

10+ Best Free Portfolio & Lookbook Templates for InDesign in 2025 — Speckyboy

by admin
June 20, 2025
User Guide For CS-Cart Product Search By Barcode
Softwares

User Guide For CS-Cart Product Search By Barcode

by admin
June 18, 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
Pamela Anderson raves about new natural, makeup-free look: ‘It’s freedom’

Pamela Anderson raves about new natural, makeup-free look: ‘It’s freedom’

October 8, 2023
Alec Baldwin indicted again for ‘Rust’ shooting that left cinematographer dead – National

Alec Baldwin indicted again for ‘Rust’ shooting that left cinematographer dead – National

January 21, 2024
I Tried Calocurb For 90 Days. Here’s My Review.

I Tried Calocurb For 90 Days. Here’s My Review.

January 8, 2025
A look into CAMPUS, ShopBack’s new Singapore HQ at Pasir Panjang

A look into CAMPUS, ShopBack’s new Singapore HQ at Pasir Panjang

July 2, 2022
Guide for Bagisto Quick Commerce

Guide for Bagisto Quick Commerce

October 16, 2024
User Manual for Odoo Docx Report Builder

User Manual for Odoo Docx Report Builder

November 30, 2024
Aaron Rodgers returns to ‘Pat McAfee Show’ 1 day after being axed by host – National

Aaron Rodgers returns to ‘Pat McAfee Show’ 1 day after being axed by host – National

January 11, 2024
8BitDo Retro Mechanical Keyboard C64 Review

8BitDo Retro Mechanical Keyboard C64 Review

March 24, 2025
Jeff Bezos and Lauren Sanchez wedding live updates: all the guests attending the $10 million ceremony

Jeff Bezos and Lauren Sanchez wedding live updates: all the guests attending the $10 million ceremony

June 25, 2025
Brittany Cartwright Accuses Jax Taylor of Drinking, Partying Following Rehab Stint

Brittany Cartwright Accuses Jax Taylor of Drinking, Partying Following Rehab Stint

June 25, 2025
TikTok Looks to Gamify Livestream Engagement With Fan Clubs

TikTok Looks to Gamify Livestream Engagement With Fan Clubs

June 25, 2025
Who Is Karmelo Anthony? About His Case & Updates on His Trial – Hollywood Life

Who Is Karmelo Anthony? About His Case & Updates on His Trial – Hollywood Life

June 25, 2025
Over 140 People ‘Pricked’ With Syringes at Music Festival

Over 140 People ‘Pricked’ With Syringes at Music Festival

June 24, 2025
Samsung Galaxy Watch 8 Classic: Battery, Design, and More

Samsung Galaxy Watch 8 Classic: Battery, Design, and More

June 24, 2025
Shane Gillis Set As Host Of The 2025 ESPYS For ABC & ESPN+

Shane Gillis Set As Host Of The 2025 ESPYS For ABC & ESPN+

June 24, 2025
Cobra Kai star Martin Kove ‘kicked out of fan convention’ after allegedly ‘biting’ co-star Alicia Hannah-Kim

Cobra Kai star Martin Kove ‘kicked out of fan convention’ after allegedly ‘biting’ co-star Alicia Hannah-Kim

June 24, 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

  • Jeff Bezos and Lauren Sanchez wedding live updates: all the guests attending the $10 million ceremony
  • Brittany Cartwright Accuses Jax Taylor of Drinking, Partying Following Rehab Stint
  • TikTok Looks to Gamify Livestream Engagement With Fan Clubs
  • 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.

New Self New Life