Sort 31 Discussions, By:
Please Login in order to post a comment
I guess we're not allowing negative values of n for P_n = n * (3n - 1)? Otherwise, the answer to the test case would also include 117, since P_-11 = 187 = 117 + 70 = P_9 + P_7.
Output Format: Print the pentagonal numbers corresponding to the test case in sorted order, each in a new line.
Формат выходных данных: Вывести пятиугольные числа для заданного теста через пробел.
Russian version is wrong.
Test Case #3
- Must remove duplicate values.
If you are using C++, make sure you are using long for all data type. Mixing long and int will give your wrong results.
The maximum number is 10^6, you can just build a Pentagon numbers map from 1 to 10^6. Then to test if Pn - Pn-k or Pn - Pn+k in this map, if either of them in the map, just print Pn.
After you build this map, you can get the results in O(1) time complexity.
For everybody who are struggling with timeout problem.Dont go through the loop to check whether a given number is pentagonal number.Just check,
n=(sqrt(24*x+1)+1)/6; if x is a pentagonal number then n should be a natural number.
Hope this helps!!!