Requesting help on 192A.

Revision en1, by Dreamzers, 2019-06-27 20:29:18

In this problem ([192A Funky Numbers](https://mirror.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.

Tags math, sqrt, #math, #help

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Dreamzers 2019-06-27 20:29:18 827 Initial revision (published)