Java examples for java.lang:Math Algorithm
get Sum Of Factors
//package com.java2s; import java.util.*; public class Main { public static void main(String[] argv) throws Exception { int i = 2; System.out.println(getSumOfFactors(i)); }/*from w w w . j a v a 2 s. c o m*/ public static Integer getSumOfFactors(int i) { List<Integer> factors = getFactors(i); int sum = 0; for (Integer integer : factors) { sum += integer.intValue(); } return Integer.valueOf(sum); } public static List<Integer> getFactors(int i) { List<Integer> factors = new ArrayList<>(); factors.add(new Integer("1")); int sqr = (int) Math.sqrt(i) + 1; for (int x = 2; x < sqr; x++) { int factor = i / x; if (i % x == 0) { factors.add(new Integer(x)); if (x != factor) { factors.add(new Integer(factor)); } } } return factors; } }