Advertisement
rahat14

Untitled

Sep 20th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. // C++ program to find GCD of two numbers
  2. #include <iostream>
  3. using namespace std;
  4. // Recursive function to return gcd of a and b
  5. int gcdd(int a, int b)
  6. {
  7. if (b == 0)
  8. return a;
  9. return gcdd(b, a % b);
  10.  
  11. }
  12.  
  13. unsigned gcd_recursive(unsigned a, unsigned b)
  14. {
  15. if (b)
  16. return gcd_recursive(b, a % b);
  17. else
  18. return a;
  19. }
  20. // Driver program to test above function
  21. int main()
  22. {
  23. int a = 3, b = 1 , sum = 0 , gcdd ;
  24. int tc ;
  25. cin>> tc ;
  26.  
  27. for(int i = 0 ; i<tc ; i++)
  28. {
  29. cin>> a ;
  30. for ( int y = 1 ; y<= a-1 ; y++)
  31. {
  32.  
  33. gcdd = gcd_recursive(a, y ) ;
  34. if ( gcdd>1 )
  35. {
  36. sum = gcdd + sum ;
  37.  
  38. }
  39.  
  40. }
  41. cout<<sum<<endl;
  42.  
  43. }
  44.  
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement