2
7/21/2019 Solution of CommonPrimeDivisors http://slidepdf.com/reader/full/solution-of-commonprimedivisors 1/2 class Solution { static boolean f(int a, int b){ boolean uslov; if(a==1&& b==1) return true; else if(a==1) return false; else if(b==1) return false; int k = 2; while(a>1&&b>1){ if((a%k==0&&b%k!=0)||(a%k!=0&&b%k==0))//jedan jeste drugi nije return false; while(a%k==0&&b%k==0)//skidamo obojici koliko moze { a/=k; b/=k; } while(a%k==0)//skidamo prvom jos koliko moze { a/=k; } while(b%k==0)//skidamo drugom jos koliko moze { b/=k; }

Solution of CommonPrimeDivisors

Embed Size (px)

DESCRIPTION

CommonPrimeDivisors exercise

Citation preview

7/21/2019 Solution of CommonPrimeDivisors

class Solution {

static boolean f(int a, int b){

boolean uslov;

if(a==1&& b==1)

return true;

else if(a==1) return false;

else if(b==1) return false;

int k = 2;

while(a>1&&b>1){

if((a%k==0&&b%k!=0)||(a%k!=0&&b%k==0))//jedan jeste drugi nije

return false;

while(a%k==0&&b%k==0)//skidamo obojici koliko moze

{

a/=k;

b/=k;

}

while(a%k==0)//skidamo prvom jos koliko moze

{

a/=k;

}

while(b%k==0)//skidamo drugom jos koliko moze

{

b/=k;

}

7/21/2019 Solution of CommonPrimeDivisors

if(a==1&&b==1) return true;

if(a==1&&b!=1) return false;

if(a!=1&&b==1) return false;

k++;

}

return true;

}

public int solution(int[] A, int[] B) {

// write your code in Java SE 8

int n = A.length;

int ukupno = 0;

for(int i = 0; i < n; i++)

if(f(A[i],B[i]))

ukupno++;

return ukupno;

}

}