Cho số tự nhiên n. Hãy liệt kê tất cả các số nguyên tố nhỏ hơn n.


Cho số tự nhiên n. Hãy liệt kê tất cả các số nguyên tố nhỏ hơn n.
 #include<iostream.h>
#include<math.h>
int ngto(int a)
{
    if (a < 2) return 0;
    if(a==2) return 1;  //Code design by SEO ANH



    else{
         for(int i = 2; i <= sqrt (a); i++){
                 if (ngto(i) && a%i == 0) return 0;
                 }
         return 1;
         }
}

#include<iostream.h>
#include <conio.h>
#include <math.h>


/*void nguyentonhohonn(int n)
{
    n>1?nguyentonhohonn(n-1):nguyento(n)?cout<<n:0:0;
}*/

int main()
{  
    int n;
    cout<<"nhap vao n: \n";
    cin>>n;
  
    cout<<"cac so nguyen to nho hon n la:\n";
    for(int k=0;k<n-1;k++)
        if(ngto(k)==1) cout<<k<<" ";
    getch();
    return 0;
  
}



Nhãn bài viết: |
Blog, Updated at: 15:25