샤브의 블로그 RSS 태그 관리 글쓰기 방명록
2010-11-09 15:29:04
public class Math {
/**
* @author sharb
* @see this function announced whether the number is prime number or not
*/
public static boolean isPrime(long num) {
long sqrt = (long) java.lang.Math.sqrt(num) + 1;
for (long i = 2; i < sqrt; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

/**
* @author sharb
* @see this function is returned the number of Factorial number
* @return type long
*/
public static long getFactorial(long l) {
long result = 1;

if (l < 2)
return 1;

for (int j = 2; j < l + 1; j++) {
result *= j;
}
return result;
}

/**
* @author sharb
* @see this function is returned the number of Factorial number
* @return type BigInteger
*/
public static BigInteger getFactorial(BigInteger l) {
BigInteger result = new BigInteger("1");

if (l.intValue() < 2)
return result;

BigInteger j = new BigInteger("2");
BigInteger temp = l.add(new BigInteger("1"));
for (; temp.compareTo(j) > 0; j.add(new BigInteger("1"))) {
// result *= j;
result = result.multiply(j);
}
return result;
}

/**
* @author sharb
* @see this function shows to you whether num_origin equals to its
*      factorial number of each num or not
*/
public static boolean isCuriousNum(long num_origin) {
long sum = 0;
long num = num_origin;

while (num > 0) {
sum += getFactorial(num % 10);
num = num / 10;
}
if (num_origin == sum) {
return true;
} else {
return false;
}
}

public static long getDivisor(long num) {
long sqrt = (long) java.lang.Math.sqrt(num) + 1;
for (long i = 2; i < sqrt; i++) {
if (num % i == 0) {
return i;
}
}

return num;
}

/**
* @author sharb
*/
public static Map getDivisors(long num) {
Map map = new TreeMap();

// 약수를 만들어낼 변수
long rotateNum = num;
// 약수
long divisor = 0;
// 약수가 반복된 횟수
long value = 0;

// for문 돌면서 약수 집어 넣기~
for (; rotateNum > 1;) {
divisor = getDivisor(rotateNum);

// 기존에 있을때
if (map.containsKey(Long.toString(divisor))) {

value = Long.valueOf((String) map.get(Long.toString(divisor)))
.longValue();
map.remove(Long.toString(divisor));

// 기존에 없을때
} else {
// map.put(Long.toString(divisor), "1");
value = 0;

}

++value;
map.put(Long.toString(divisor), Long.toString(value));

rotateNum = rotateNum / divisor;
}

return map;
}
public static long countDivisors(long num) {
Map map = getDivisors(num);
long temp = 1 ;
Iterator it = map.values().iterator();
while(it.hasNext()){
temp *= (Long.parseLong((String)it.next())+1);
}
return temp;
}
public static long getTriangleNumber(long sequence){
long result = 0;
for(long j = 1 ; sequence>0; ){
result += j ;
sequence--;
j++;
}
return result;
}
}

'Device & Language' 카테고리의 다른 글

C# IP 확인시...  (0) 2016.05.31
웹사이트 최적화 방법론  (0) 2013.10.17
Data compression in DB2 9  (0) 2013.08.20