We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #44: Pentagon numbers
You are viewing a single comment's thread. Return to all comments →
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.