Advertisement
Guest User

binomial theorem

a guest
Oct 29th, 2012
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. //Maciej Januszewski, III semestr, PS2
  5.  
  6. //Wyznacza wartość symbolu Newtona z definicji(iteracyjnie).
  7. //Najpierw trzeba zdefiniować własność silni.
  8. long silnia(int a)
  9. {
  10.     long s;
  11.    
  12.     //Z własności wiemy, że 1!=1 oraz 0! = 1
  13.     if (a == 0 || a == 1)
  14.         {
  15.         return 1;
  16.         }
  17.         else
  18.         {
  19.         s = 1;
  20.         for (int i = 1; i <= a; i++)
  21.         {
  22.             s *= i;
  23.         }
  24.         }
  25.     return s;
  26. }
  27.  
  28. //Mając daną już własność silni, korzystam ze wzoru.
  29. long newton(int n, int k)
  30. {
  31.         return silnia(n)/(silnia(k)*silnia(n-k));
  32. }
  33.  
  34. //Wyznacza wartość  symbolu Newtona rekurencyjnie ze wzoru.
  35. unsigned long int newton_rek(long int n ,long int k)
  36. {
  37.     if ( n == k || k == 0 )
  38.     {
  39.         return 1;
  40.     }
  41.    
  42.     if (k > n)
  43.     {
  44.         return 0;
  45.     }
  46.    
  47.     else return newton_rek(n-1,k-1) + newton_rek(n-1,k);
  48. }
  49. //Program główny.
  50. int main()
  51. {
  52.     int n = 0;
  53.     int k = 0;
  54.     long funkcja1 = 0;
  55.     long funkcja2 = 0;
  56.  
  57.                  FILE *f = fopen("In0101.txt", "r+");    
  58.                  if (f == NULL)
  59.                         {
  60.                                 printf("Nie udalo sie otworzyc pliku In0101.txt\n");
  61.                                 return 1;
  62.                         }
  63.                         fread(n, sizeof(long), 1 , f);
  64.                         fread(k, sizeof(long), 1 , f);
  65.                         fclose(f);
  66.                        
  67.                         FILE *ff = fopen("Out0101.txt", "w+");    
  68.                            
  69.                         if (ff == NULL)
  70.                              {
  71.                                 printf("Nie udalo sie otworzyc pliku Out0101.txt\n");
  72.                                 return 1;
  73.                              }
  74.  
  75.                         funkcja1 = newton(n,k);
  76.                         funkcja2 = newton_rek(n,k);
  77.                         fwrite(funkcja1, sizeof(long), 1 , ff);
  78.                         fwrite(funkcja2, sizeof(long), 1 , ff);
  79.                         fclose(f);
  80.                        
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement