From 2aec4efdd3839cd513218b1738d13e6a324c18fe Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Tue, 23 Jun 2020 23:50:45 +0530 Subject: [PATCH] fix: integer overflow due to multiplication fixed (#886) * formatting source-code for 72c365dcd34d9776fac3e2b58b41891b3619b02e * Fixed Bug [munmap_chunck() core dumped] * formatting source-code for b06bbf4dc6c46a3284d7852bb570438384eef4ef * fixed line spacing * fixed line spacing * fixed documentation * closed the paranthesis of line 3 * formatting source-code for 8233eda8894f46785f288e72c639d201852f6096 * Bug Fix heap sort [Fresh Implementation] * formatting source-code for e464ddac3688834ce46c59bada5ad3fddbfa2254 * Bug Fix heap sort [Fresh Implementation] * formatting source-code for 803981c831b36bf940d3fa2a901340cb0029717b * switched to normal functions from lambda * formatting source-code for ced5dcd6c4db53ece27d22ecd1dd1c1fcce6afb0 * Added template and test cases * formatting source-code for 7c8617fa46d41481c68ae2ae9d4f6a89ca42a4ff * fixed docs * fixed line spacing in tests * fix docs * Multiplication result may overflow 'int' before it is converted to 'long'. * fixed cpplint long -> int64 * fixed compiler error Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- math/primes_up_to_billion.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/math/primes_up_to_billion.cpp b/math/primes_up_to_billion.cpp index 4fb79a15e..b7377cea5 100644 --- a/math/primes_up_to_billion.cpp +++ b/math/primes_up_to_billion.cpp @@ -14,9 +14,9 @@ void Sieve(int64_t n) { memset(prime, '1', sizeof(prime)); // intitize '1' to every index prime[0] = '0'; // 0 is not prime prime[1] = '0'; // 1 is not prime - for (int p = 2; p * p <= n; p++) { + for (int64_t p = 2; p * p <= n; p++) { if (prime[p] == '1') { - for (int i = p * p; i <= n; i += p) + for (int64_t i = p * p; i <= n; i += p) prime[i] = '0'; // set all multiples of p to false } }