thieumao

Phân tích 1 số ra thừa số các số nguyên tố

Oct 11th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4.  
  5. ntotest (int x)
  6. {
  7.     int i,kq=1;
  8.     if (x>3)
  9.     {
  10.         for (i=2;i<=pow(x,0.5);i++)
  11.         {
  12.             if (x%i==0) kq=0;
  13.         }
  14.     }
  15.     return(kq);
  16. }
  17.  
  18. void main()
  19. {
  20.     int n,i,dem=0,m,last,nto[1000],ntodem[1000];
  21.     printf("nhap n:");scanf("%d", &n);
  22.     for (i=1;i<=n;i++)
  23.     {
  24.         if (ntotest(i)==1)
  25.         {
  26.             dem++;nto[dem]=i;
  27.         }
  28.     }
  29.     m=n;
  30.     for (i=2;i<=dem;i++) ntodem[i]=0;
  31.     for (i=2;i<=dem;i++)
  32.     {
  33.         while (m%nto[i]==0)
  34.         {
  35.             m=m/nto[i];ntodem[i]++;last=i;
  36.         }
  37.     }
  38.     printf("ket qua la: ");
  39.     for (i=2;i<=dem;i++)
  40.     {
  41.         if (ntodem[i]!=0)
  42.         {
  43.             printf("%d^%d", nto[i], ntodem[i]);
  44.             if (i<last)
  45.             printf ("x");
  46.         }
  47.     }
  48.     getch();
  49. }
Advertisement
Add Comment
Please, Sign In to add comment