Dreamzers's blog

By Dreamzers, history, 22 months ago, In English

In this problem ([192A Funky Numbers](https://codeforces.com/contest/192/problem/A)), given an integer 'n', find if it can be the sum of two triangular numbers where a triangular number equals k(k+1)/2 for some positive integer k.

I have two questions regarding this problem.

  1. In many solutions I saw, people would increment and int i from i to Math.sqrt(n) and use this value to find the k value for the second triangular number by using the equation n — (i(i+1)/2). Why stop at Math.sqrt(n)?

  2. Following the above algorithm, k2 (the k of the second triangular number) could be found as such:

int k2 = (int)(Math.sqrt((n — (i*(i+1)/2)) * 2));

This time, why is Math.sqrt() used, and why is the result multiplied by 2?

Thank you for your help, Codeforces community.

Read more »

  • Vote: I like it
  • 0
  • Vote: I do not like it