From c344d8f6f155cca62c183327a0ccaa6e541a4bea Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Tue, 23 Jun 2020 22:58:44 +0530 Subject: [PATCH] Multiplication result may overflow 'int' before it is converted to 'long'. --- 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 b7377cea5..af6d34ff3 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 (int64_t p = 2; p * p <= n; p++) { + for (long p = 2; p * p <= n; p++) { if (prime[p] == '1') { - for (int64_t i = p * p; i <= n; i += p) + for (long i = p * p; i <= n; i += p) prime[i] = '0'; // set all multiples of p to false } }