nocturnalmk

lab5-4

Nov 30th, 2011
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. /*
  2.     Да се направи програма за факторизација на природен број n. Факторизација на природен
  3.     број е постапка на претставување на бројот како производ од прости фактори, т.е производ
  4.     од сите прости броеви на некој степен.
  5. */
  6.  
  7. #include <stdio.h>
  8.  
  9. int prost(int x) {
  10.  
  11.     int i;
  12.  
  13.     for (i=2; i<=x/2; i++) {
  14.         if (x%i == 0) {
  15.             return 0;
  16.         }
  17.     }
  18.  
  19.     return 1;
  20.  
  21. }
  22.  
  23. int main() {
  24.  
  25.     int broj;
  26.     int i,del,stepen;
  27.  
  28.     printf("Vnesi broj: ");
  29.     scanf("%d", &broj);
  30.  
  31.     printf("\n\n%d = ", broj);
  32.  
  33.     for (i=2; i<=broj; i++) {
  34.  
  35.         stepen = 0;
  36.  
  37.         if (prost(i) == 1) {
  38.  
  39.             while (broj%i == 0) {
  40.  
  41.                 stepen++;
  42.                 broj /= i;
  43.  
  44.             }
  45.  
  46.             if (stepen != 0) {
  47.                 printf("%d^%d * ", i, stepen);
  48.             }
  49.  
  50.         }
  51.  
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment