对象已移动

可在此处找到该文档 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
3 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

Fixes, Polish, and security updates – Vivaldi Browser snapshot 3813.3
Softwares

Fixes, Polish, and security updates – Vivaldi Browser snapshot 3813.3

by admin
September 19, 2025
Fintech Software Development in 2025: Your Complete Guide
Softwares

Fintech Software Development in 2025: Your Complete Guide

by admin
September 20, 2025
User Guide for Odoo Zoho Analytics Connector
Softwares

User Guide for Odoo Zoho Analytics Connector

by admin
September 16, 2025
30+ Best Business & Corporate Report Templates for InDesign & Photoshop in 2025 — Speckyboy
Softwares

30+ Best Business & Corporate Report Templates for InDesign & Photoshop in 2025 — Speckyboy

by admin
September 18, 2025
Software tool turns everyday objects into animated, eye-catching displays—without electronics
Softwares

Software tool turns everyday objects into animated, eye-catching displays—without electronics

by admin
September 17, 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
I Only Have More Questions After Another Bizarre Outing With The Harrigans

I Only Have More Questions After Another Bizarre Outing With The Harrigans

April 20, 2025
Amazon Forgot to Take the 2024 MacBook Air Off Sale After Their Big Spring Event

Amazon Forgot to Take the 2024 MacBook Air Off Sale After Their Big Spring Event

April 4, 2025
Google’s AI Ambitions An ‘Existential Crisis’ For News Online

Google’s AI Ambitions An ‘Existential Crisis’ For News Online

September 6, 2025
Ecca Vandal’s “CRUISING TO SELF SOOTHE” video is an ode to skate culture

Ecca Vandal’s “CRUISING TO SELF SOOTHE” video is an ode to skate culture

March 10, 2025
Easy Blueberry Scones (With Frozen Blueberries)

Easy Blueberry Scones (With Frozen Blueberries)

April 10, 2025
Instagram Adds New Teleprompter Tool To Edits

Instagram Adds New Teleprompter Tool To Edits

June 11, 2025
Acyan's "Ghost Town" EP Is Bass Music Storytelling at Its Most Ominous

Acyan's "Ghost Town" EP Is Bass Music Storytelling at Its Most Ominous

May 18, 2025
The Most Visited Websites in the World [Infographic]

The Most Visited Websites in the World [Infographic]

May 12, 2025
The ‘Lego Batman’ Devs Want to Make a New, Definitive Bat-Game

The ‘Lego Batman’ Devs Want to Make a New, Definitive Bat-Game

September 20, 2025
Natalie Wood’s lookalike daughter and granddaughter stun in new photos inspired by the late star

Natalie Wood’s lookalike daughter and granddaughter stun in new photos inspired by the late star

September 20, 2025
Win An Iconic Luxury Watch With The Premium Time Company

Win An Iconic Luxury Watch With The Premium Time Company

September 20, 2025
Alabama Barker Reveals She Was Pregnant But ‘Lost’ The Baby In Deleted TikTok! Oh No!

Alabama Barker Reveals She Was Pregnant But ‘Lost’ The Baby In Deleted TikTok! Oh No!

September 20, 2025
Inside the Rebuild of Amnesia’s Legendary Ibiza Sound System

Inside the Rebuild of Amnesia’s Legendary Ibiza Sound System

September 20, 2025
Inside Matthew McConaughey’s ‘Poems & Prayers’ revival book tour

Inside Matthew McConaughey’s ‘Poems & Prayers’ revival book tour

September 20, 2025
See the Credits and Lyrics to Aerosmith’s New Song With Yungblud

See the Credits and Lyrics to Aerosmith’s New Song With Yungblud

September 19, 2025
The best live TV streaming services to cut cable in 2025

The best live TV streaming services to cut cable in 2025

September 19, 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

  • The ‘Lego Batman’ Devs Want to Make a New, Definitive Bat-Game
  • Natalie Wood’s lookalike daughter and granddaughter stun in new photos inspired by the late star
  • Win An Iconic Luxury Watch With The Premium Time Company
  • 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