- iterativo
- long int gcd(long int a, long int b)
- {
- long int tmp;
- if(b > a)
- {
- tmp = a;
- a = b;
- b = tmp;
- }
- while(b != 0)
- {
- tmp = a;
- a = b;
- b = tmp % b;
- }
- return a;
- }
- int gcd(int a,int b){
- if(a==0)
- return b;
- else if(b==0)
- return a;
- else if(a==b)
- return a;
- if (a > b)
- return gcd(a-b, b);
- return gcd(a, b-a);
- }
- ll gcd(ll val1,ll val2){
- while(true){
- if(val1 == val2) return val1;
- else if(val1 == 0) return val2;
- else if(val2 == 0) return val1;
- if(val1 > val2){
- val1 = val1 - val2;
- }
- else if(val2 > val1){
- val2 = val2 - val1;
- }
- }
- }
- ll lcm(ll val1,ll val2){
- return (val1 * val2) / gcd(val1,val2);
- }
- int lcm(int a,int b){
- return a*b/gcd(a,b);
- }
propiedad:
lcm(a, b) = (a * b) / gcd(a, b);
- int lcm(int a, int b)
- {
- int large = max(a, b);
- int small = min(a, b);
- for (int i = large; ; i += large)
- {
- if (i % small == 0)
- {
- return i;
- }
- }
- }
- static int lcm (int a, int b) {
- return a / gcd(a, b) * b;
- }
- static int gcd(int a, int b) {
- return b == 0 ? a : gcd(b, a % b);
- }
- private static int gcd(int a, int b) {
- if (a == 0)
- return b;
- return gcd(b % a, a);
- }
- private static int lcm(int a, int b) {
- return (a * b) / gcd(a, b);
- }
- public static int findLcm(int a, int b){
- if(b%a==0) {
- return b;
- }
- for(int i = 2; i<a; i++) {
- if ((b*i)%a==0) {
- return b*i;
- }
- }
creo que me quedo con este
- public static int gcd(int a, int b){
- if (b == 0)
- return a;
- return gcd(b, a % b);
- }
- public static int lcm(int a,int b){
- int g=gcd(a,b);
- return (a*b)/g;
- }
- }
- public static int GCD(int x, int y){
- int gcd = 1;
- for(int i = 1; i <= x && i <= y; ++i)
- {
- if(x % i==0 && y % i==0)
- gcd = i;
- }
- return gcd;
- }
- public static int LCM(int z, int k){
- return (z*k)/GCD(z,k);
- }
No hay comentarios:
Publicar un comentario