Skip to content

Instantly share code, notes, and snippets.

@jpoetker
Created February 12, 2012 20:49
Show Gist options
  • Save jpoetker/1810790 to your computer and use it in GitHub Desktop.
Save jpoetker/1810790 to your computer and use it in GitHub Desktop.
Emebdly R
import java.math.BigDecimal;
public class R {
public static BigDecimal factorial(BigDecimal n) {
if (n.equals(BigDecimal.ONE)) {
return BigDecimal.ONE;
}
return n.multiply(factorial(n.subtract(BigDecimal.ONE)));
}
public static BigDecimal r(BigDecimal n) {
BigDecimal f = factorial(n);
BigDecimal r = new BigDecimal(0);
while(f.compareTo(BigDecimal.ZERO) > 0) {
BigDecimal digit = f.remainder(BigDecimal.TEN);
r = r.add(digit);
f = f.subtract(digit);
f = f.divide(BigDecimal.TEN);
}
System.out.println("R(" + n + ") = " + r);
return r;
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("R(10) = " + r(BigDecimal.TEN).toString());
BigDecimal answer = new BigDecimal(8001);
BigDecimal n = new BigDecimal(4);
while (r(n).compareTo(answer) != 0) {
n = n.add(BigDecimal.ONE);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment