Guest User

Untitled

a guest
Oct 16th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. public int solution100Gcd(int[] A, int[] B){
  2. int r = 0;
  3.  
  4. for(int i = 0; i < A.length; i++){
  5. int a = A[i], b = B[i];
  6. if(a == b){
  7. r++;
  8. } else {
  9. int gcd = gcd(a, b);
  10. if( restFromDividingByGcd(a, gcd) == 1 &&
  11. restFromDividingByGcd(b, gcd) == 1){
  12. r++;
  13. }
  14. }
  15. }
  16. return r;
  17. }
  18.  
  19. int restFromDividingByGcd(int number, int gcd_value){
  20. while(gcd_value != 1){
  21. number /= gcd_value;
  22. gcd_value = gcd(number, gcd_value);
  23. }
  24.  
  25. return number;
  26. }
  27.  
  28. int gcd(int a, int b) {
  29. if(a % b == 0)
  30. return b;
  31. else
  32. return gcd(b, a % b);
  33. }
Add Comment
Please, Sign In to add comment