Advertisement
Guest User

Pi computing

a guest
Apr 1st, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. unsigned long long pow(int x, int y)
  5. {
  6.     unsigned long long res;
  7.     res = x;
  8.     if(y<1)
  9.     {
  10.         return 1;
  11.     }
  12.     else
  13.     {
  14.         for(int i=1;i<y;i++)
  15.         {
  16.             res=res*x;
  17.         }
  18.         return res;
  19.     }
  20. }
  21.  
  22.  
  23. int main(void)
  24. {
  25.     unsigned long long  main_mass=10;
  26.     int ratio, secondary_mass=1;
  27.     double main_vel=1, secondary_vel=0, vcm;
  28.     unsigned long long  collisions=0;
  29.     do
  30.     {
  31.         printf("input the weight of the main mass(positive) A, so that weight=100^A, the secondary mass is 1kg");
  32.         scanf("%d",&ratio);
  33.         main_mass=pow(100,ratio);
  34.     }
  35.     while(ratio<0);
  36.  
  37.  
  38.     do
  39.     {
  40.         //cubes collide
  41.         vcm = (main_mass*main_vel+secondary_mass*secondary_vel)/(main_mass+secondary_mass);
  42. //      printf("2*%f-%f=",vcm,main_vel);
  43.         main_vel = (2*vcm-main_vel);
  44. //      printf("%f\n",main_vel);
  45.         secondary_vel = 2*vcm-secondary_vel;
  46.         collisions++;
  47.         if(secondary_vel>0){
  48.         //wall and cube collide
  49.         secondary_vel = secondary_vel*(-1);
  50.         collisions ++;}
  51.  
  52.     }
  53.     while( secondary_vel < main_vel ||( secondary_vel>0 && main_vel > 0));
  54.     printf("%llu collisions\n",collisions);
  55.  
  56.     return 0;
  57.  
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement