Skip to content

Instantly share code, notes, and snippets.

@SiarheiFedartsou
Created March 19, 2014 12:35
Show Gist options
  • Save SiarheiFedartsou/9640762 to your computer and use it in GitHub Desktop.
Save SiarheiFedartsou/9640762 to your computer and use it in GitHub Desktop.
Какое значение вернёт вызов функции на языке Java count(32767)?
private BigInteger g(BigInteger n, BigInteger g)
{
if (g.compareTo(BigInteger.ONE) <= 0 || g.compareTo(n.subtract(BigInteger.ONE)) >= 0){
return BigInteger.ONE;
}
BigInteger result = BigInteger.ONE;
BigInteger c = BigInteger.valueOf(2);
while (c.compareTo(g) <= 0){
BigInteger d = n.subtract(g);
if (c.compareTo(d) <= 0){
result = result.add(g(d, c));
}
c = c.add(BigInteger.ONE);
}
return result;
}
public BigInteger count(int n)
{
BigInteger v = BigInteger.valueOf(n);
BigInteger result = BigInteger.valueOf(1 - v.abs().signum());
BigInteger c = BigInteger.ONE;
while (c.compareTo(v) <= 0){
result = result.add(g(v, c));
c = c.add(BigInteger.ONE);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment