Jump to content
Sign in to follow this  
Courage

Primele N Numere Prime

Recommended Posts

Enunţ: Să se afişeze primele n numere prime. Exemplu: Date de intrare n=6 Date de ieşire 2 3 5 7 11 13.

 

Prima dată mă gândeam la 

 

#include <iostream>
#include <math.h>
#include <limits>
using namespace std;
int main()
{
    unsigned n,i,d,x,s=0;
    cout<<"n=";
    cin>>n;
    for (i=2;i<=numeric_limits<double>::infinity();i++)
    {
        x=1;
        if (i%2==0 && i!=2)
            x=0;
        else
            d=3;
        while (d<=sqrt(i) && x==1)
        {
            if (i%d==0)
                x=0;
            else
                d=d+2;
        }
        if (x==1)
        {
            cout<<i<<" ";
            s++;
            if (s==n)
                break;
        }
    }
    return 0;
}
 

 

Însă am mai lucrat azi dimineaţă şi l-am mai simplificat. Acum arată aşa:

 

#include <iostream>
#include <math.h>
#include <limits>
using namespace std;
int main()
{
    unsigned n,i,x,s=0,p;
    cout<<"n=";
    cin>>n;
    for (i=2;i<=numeric_limits<double>::infinity();i++)
    {
        x=1;
        for (p=2;p<=i/2;p++)
            if (i%p==0)
                x=0;
        if (x==1)
        {
            cout<<i<<" ";
            s++;
            if (s==n)
                break;
        }
    }
    return 0;
}
 

 

E ok? Ambele afişează ceea ce trebuie :)

Share this post


Link to post
Share on other sites

dc mergi pana la infinit, si pui o conditie speciala pt n?

nu era mai logic s mergi direct pana la n?

 

for (i=2;i<=numeric_limits<double>::infinity();i++)

 

trebuia sa fie

for(i=2;i<=n;i++)

 

astfel, disparea si  s++; if (s==n) break;

 

nu mai folosii reprezentarea aia pentru infinit asa usor, nu e o idee buna.


love is a verb
Love is a doing word

Share this post


Link to post
Share on other sites

Nu trebuie pana la n, ci trebuie primele n numere. Adica daca bag, de exemplu, n=10 îmi afişează 2, 3, 5, 7 în loc de 2, 3, 5, 7, 11, 13, 17, 19, 23, 29

Share this post


Link to post
Share on other sites

ah da scz. tocmai ce mam trezit.

 

acuma e oleaca mai logic ce ai zis tu :))

 

for (i=2;i<=numeric_limits<double>::infinity();i++)

tot numi place ala, as fi facut ceva gen for(i=2;;i++)  astfel nu e nici o conditie de testat, e ca si cand ai pune for(i=2;true;i++)

pt ca oricum astepti "break" ca sa iesi..

sau direct 

for(i=2;s < n;i++)

eh, e dar cum as face eu, e bun si al tau. scz pt greseala de inainte :))


love is a verb
Love is a doing word

Share this post


Link to post
Share on other sites

O.O Deci pot baga in loc de numeric_limits<double>::infinity(), n>1 ??

 

Thx!!

 

Acum arata asa:

 

#include <iostream>
using namespace std;
int main()
{
    unsigned n,i,x,s=0,p;
    cout<<"n=";
    cin>>n;
    for (i=2;i>1;i++)
    {
        x=1;
        for (p=2;p<=i/2;p++)
            if (i%p==0)
                x=0;
        if (x==1)
        {
            cout<<i<<" ";
            s++;
            if (s==n)
                break;
        }
    }
    return 0;
}

Share this post


Link to post
Share on other sites

tiam zis, poti sal lasi liber...poti folosii un while(true) in loc de primul for, facand ceva d genu

i=1;

while(true)

{

i++;                          

//aici e tot codu tau de dupa ce ai citit n

}


love is a verb
Love is a doing word

Share this post


Link to post
Share on other sites

While(true) e folosit practic cand stii ca e adevarat, va fii mereu adevarat, se va repeta la infinit. O opresti cu break.

if(n) e echivalent cu if(n!=0)


fWy1P.gif

 

Share this post


Link to post
Share on other sites

OK :).

 

Deci mai simplu decat

#include <iostream>
using namespace std;
int main()
{
    unsigned n,i=1,x,s=0,p;
    cin>>n;
    while (true)
    {
        i++;
        x=1;
        for (p=2;p<=i/2;p++)
            if (i%p==0)
                x=0;
        if (x==1)
        {
            cout<<i<<" ";
            s++;
            if (s==n)
                break;
        }
    }
    return 0;
}

se poate?

Share this post


Link to post
Share on other sites

while(true)......break <=> while(ok==0)........ok=1

E bine asa.


fWy1P.gif

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...