Giúp giải bài tập C: dùng hàm để tìm N chính là số nguyên tố

dùng hàm để tìm N chính là số nguyên tố
 

VSupport

Ngây thơ trong tối
Đây nè bạn:
Mã:
int KiemtraNguyento(int n)
{
        if(n<2) //Nếu n nhỏ hơn 2 thì ko phải số nguyen tố nên return 0
           return 0;
        if(n==2)
           return 1; //2 là số nguyên tố
        else
           for(int i=2;i<n;i++)
           {
                if(n%i==0)
                    return 0;//Nếu chia hết cho 1 số nào trong khoảng 2->n thì return 0
           }
           //Chạy hết từ i->n nhưng vẫn ko chia hết cho số nào thì số là số ntố
            return 1;  
}
void main()
{
       int dem=0;
       for(int i=0;i<10000;i++)
       {
             if(KiemtraNguyento(i)!=0)
              {
                  dem=dem+1;
         printf("%d\t",i);
              }
       }
       printf("Co tat ca %d so nguyen to",dem);
}
 

aloxinh_nb

It's Secret !
Mã:
[FONT=Consolas][COLOR=#333333][B]bool SoNT[/B]( int N)
[/COLOR][/FONT]{
  if (N <2)
     return failse;
  else
     {
        if ( N = 2)
            return true;
        else
          {
              for (i = 3; i<N/2; i++)
                {
                    if ( N % i)
                      return false;
                }
          }
     }
   return true; 
}
 

taplamhacker

♥ Thanh Trâm ♥
Mã:
[FONT=Consolas][COLOR=#333333][B]bool SoNT[/B]( int N)
[/COLOR][/FONT]{
  if (N <2)
     return failse;
  else
     {
        if ( N = 2)
            return true;
        else
          {
              for (i = 3; i<N/2; i++)
                {
                    if ( N % i)
                      return false;
                }
          }
     }
   return true; 
}
cái này chạy sai nha a ~~
dù e sửa code cho chính xác
Mã:
bool SoNT( int N)
{
  if (N <2)
     return false;
  else
     {
        if ( N == 2)
            return true;
        else
          {
              for (int i = 3; i<N/2; i++)
                {
                    if ( N % i == 0)
                      return false;
                }
          }
     }
   return true; 
}


:conan:

bạn thớt tham khảo
Mã:
bool CheckSNT(int n)
{
    if(n<2)
        return false;
    if(n==2||n==3)
        return true;
    if(n % 2== 0 || n % 3 == 0)
        return false;
    for(int i = 5; i < sqrt(n) ; i += 2)
        if(n % i ==0)
            return false;
    return true;
}
 

Joinal457

Phang bừa :v
cái này chạy sai nha a ~~
dù e sửa code cho chính xác
Mã:
bool SoNT( int N)
{
  if (N <2)
     return false;
  else
     {
        if ( N == 2)
            return true;
        else
          {
              for (int i = 3; i<N/2; i++)
                {
                    if ( N % i == 0)
                      return false;
                }
          }
     }
   return true; 
}


:conan:

bạn thớt tham khảo
Mã:
bool CheckSNT(int n)
{
    if(n<2)
        return false;
    if(n==2||n==3)
        return true;
    if(n % 2== 0 || n % 3 == 0)
        return false;
    for(int i = 5; i < sqrt(n) ; i += 2)
        if(n % i ==0)
            return false;
    return true;
}
ma trận:waaaht:
 

aloxinh_nb

It's Secret !
cái này chạy sai nha a ~~
dù e sửa code cho chính xác
Mã:
bool SoNT( int N)
{
  if (N <2)
     return false;
  else
     {
        if ( N == 2)
            return true;
        else
          {
              for (int i = 3; i<N/2; i++)
                {
                    if ( N % i == 0)
                      return false;
                }
          }
     }
   return true; 
}


:conan:

bạn thớt tham khảo
Mã:
bool CheckSNT(int n)
{
    if(n<2)
        return false;
    if(n==2||n==3)
        return true;
    if(n % 2== 0 || n % 3 == 0)
        return false;
    for(int i = 5; i < sqrt(n) ; i += 2)
        if(n % i ==0)
            return false;
    return true;
}


code của e khác gì đâu :-?
 
Top