Primes in numbers

kata programming

مساله:

با توجه به عدد مثبت n > 1، تجزیه عامل اصلی n را پیدا کنید. نتیجه یک رشته با فرم زیر خواهد بود:

 "(p1**n1)(p2**n2)...(pk**nk)"

ترتیب قرار گیری باید به صورت افزایش p(i) باشد و اگر n(i) برابر یک باشد باید آنرا خالی بگذارید.

Example: n = 86240 should return "(2**5)(5)(7**2)(11)"

Given a positive number n > 1 find the prime factor decomposition of n. The result will be a string with the following form :

 "(p1**n1)(p2**n2)...(pk**nk)"

with the p(i) in increasing order and n(i) empty if n(i) is 1.

Example: n = 86240 should return "(2**5)(5)(7**2)(11)"

راه حل (Solution) :

public class PrimeDecomp {

    public static String factors(int lst) {
        String result = "";
        for (int fac = 2; fac <= lst; ++fac) {
            int count;
            for (count = 0; lst % fac == 0; ++count) {
                lst /= fac;
            }
            if (count > 0) {
                result += "(" + fac + (count > 1 ? "**" + count : "") + ")";
            }
        }
        return result;
    }
}
import java.util.*;
import java.util.stream.*;

public class PrimeDecomp {
  public static String factors(int n) {
    List<String> l = new ArrayList<String>();
    for (int i = 2; i <= n; i++) {
      int times = 0;
      while (n % i == 0) {
        n /= i;
        times++;
      }
      if (times == 1) l.add(Integer.toString(i));
      else if (times > 1) l.add(String.format("%d**%d", i, times));
    }
    return l.stream().collect(Collectors.joining(")(", "(", ")"));
  }
}
public class PrimeDecomp {
   
    public static String factors(int n) {
        String result = "";
        int cur = n;
        for(int i = 2; i<=cur; i++){
            int ct = 0;
            while(cur%i == 0){
              ct += 1;
              cur = cur/i;
            }
            if(ct == 1)
                result  = result + "(" + i + ")";
            else if(ct > 1)
                result  = result + "(" + i + "**" + ct + ")";
        }
        return result;
    }
       
}
import java.util.LinkedHashMap;
import java.util.Map;

public class PrimeDecomp {
  public static String factors(final int n) {
    final StringBuilder builder = new StringBuilder();
    for (final Map.Entry<Integer, Integer> factor : factorize(n).entrySet()) {
      builder.append('(').append(factor.getKey());
      final Integer exponent = factor.getValue();
      if (exponent > 1) {
        builder.append("**").append(exponent);
      }
      builder.append(')');
    }
    return builder.toString();
  }

  public static Map<Integer, Integer> factorize(int n) {
    final Map<Integer, Integer> factors = new LinkedHashMap<>();
    int divisor = 2;
    while (n > 1 && divisor * divisor <= n) {
      if (n % divisor == 0) {
        put(factors, divisor);
        n /= divisor;
      } else {
        divisor += divisor == 2 ? 1 : 2;
      }
    }
    if (n > 1) {
      put(factors, n);
    }
    return factors;
  }

  private static void put(final Map<Integer, Integer> factors, final int factor) {
    final Integer exp = factors.putIfAbsent(factor, 1);
    if (exp != null) {
      factors.put(factor, exp + 1);
    }
  }
}
import java.util.*;
public class PrimeDecomp {
  public static String factors(int n) {
    final StringBuilder fs = new StringBuilder();
    for (int i = 2; i <= n; i++) {
      int cnt = 0;
      while (n % i == 0) {
        cnt++;
        n /= i;
      }
      if (cnt > 1) {
        fs.append(String.format("(%d**%d)", i, cnt));
      } else if (cnt == 1) {
        fs.append(String.format("(%d)", i));
      }
    }
    return fs.length() == 0 ? String.format("(%d)", n) : fs.toString();
  }
}
import java.util.*;
public class PrimeDecomp {
  public static String factors(int n) {
    final StringBuilder fs = new StringBuilder();
    for (int i = 2; i <= n; i++) {
      int cnt = 0;
      while (n % i == 0) {
        cnt++;
        n /= i;
      }
      if (cnt > 1) {
        fs.append(String.format("(%d**%d)", i, cnt));
      } else if (cnt == 1) {
        fs.append(String.format("(%d)", i));
      }
    }
    return fs.length() == 0 ? String.format("(%d)", n) : fs.toString();
  }
}
import java.util.ArrayList;
import java.util.List;

public class PrimeDecomp {

  public static String factors(int lst) {
    List<String> primes = new ArrayList<String>();
    for (int number = 2; number <= lst; number++) {
      int count = 0;
      while (lst % number == 0) {
        count++;
        lst /= number;
      }

      if (count == 0)
        continue;
      
      String str = count > 1 ? "(" + number + "**" + count + ")" : "(" + number + ")";
      
      primes.add(str);
    }

    return String.join("", primes);
  }

}

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد.