# Project Euler #1: Multiples of 3 and 5

# Project Euler #1: Multiples of 3 and 5

+ 0 comments or anyone who is using Python3. If you've already converted from the iterive to the O(1) solution but you're still getting the "wrong answer" with Test Cases 2 and 3 here is why: When you divide integers in python 3, the answer is converted automatically to a float. Becuase Test Cases 2 and 3 involve very large numbers, when you convert back to integer, there will be a huge rounding error and your answers will be wrong. so rather than dividing with 2 or multiplying by 0.5 just do right shift by 1.

+ 0 comments I am getting Runtime Error for Test Case 2 & 3. The logic seems fine, can someone please help?

#!/bin/python3 import sys t = int(input().strip()) for a0 in range(t): n = int(input().strip()) p = 0 if 1 <= t <= 10 ** 5 and 1 <=n <= 10 ** 9: p = [x for x in range(1,n) if x % 3 == 0 or x % 5 == 0] print(sum(p))

+ 0 comments tried so hard to get this right :')

import sys t = int(input().strip()) for a0 in range(t): n = int(input().strip()) a = ((n-1)//3) b = ((n-1)//5) c = ((n-1)//15) answer = (3*a*(a+1)//2) + (5*b*(b+1)//2) - (15*c*(c+1)//2) print (int(answer))

+ 0 comments If you approached the question using AP and have

**removed the repeating elements (i.e., sum of multiples of 15)**from the:**sum of AP of 3 + sum of AP of 5**, then the only thing you need to make sure is that integer overflow does not occur. This is the reason why your testcases 2 & 3 might not pass. One trivial way to ensure that integer overflow doesn't occur is to use**long long int**for every variable. Another way is to type cast befor overflow occurs for storing the intermediate values. Here's my solution for your reference.#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; int main(){ int t; cin >> t; for(int a0 = 0; a0 < t; a0++){ long long int n; cin >> n; long long int a = (n - 1) / 3; long long int b = (n - 1) / 5; long long int sum = a * (3 + 3 * a) >> 1; sum += b * (5 + 5 * b) >> 1; long long int c = (n - 1) / 15; sum -= c * (15 + 15 * c) >> 1; cout << sum << "\n"; } return 0; }

+ 0 comments import sys t = int(input().strip()) for a0 in range(t): n = int(input().strip()) three=(n-1)//3 five=(n-1)//5 fifteen=(n-1)//15 print((3*(three*(three+1)//2)+5*(five*(five+1)//2)-15*(fifteen*(fifteen+1)//2)))

Sort 997 Discussions, By:

Please Login in order to post a comment