Commit b29dd2ce0f2e18e663339fdc325116c91b3e8288
1 parent
4ca83788c0
Exists in
main
Updating the directory NumberTheoryandCombinatorics.
Showing 2 changed files with 110 additions and 0 deletions Side-by-side Diff
NumberTheoryandCombinatorics/ProjectEuler/SumSquareDifference/SumSquareDifference.cpp
View file @
b29dd2c
1 | +// | |
2 | +// Created by ronal on 2/12/2023. | |
3 | +// Problem description Link. | |
4 | +// https://www.hackerrank.com/contests/projecteuler/challenges/euler006/copy-from/1354371634 | |
5 | + | |
6 | +#include <map> | |
7 | +#include <set> | |
8 | +#include <list> | |
9 | +#include <cmath> | |
10 | +#include <ctime> | |
11 | +#include <deque> | |
12 | +#include <queue> | |
13 | +#include <stack> | |
14 | +#include <string> | |
15 | +#include <bitset> | |
16 | +#include <cstdio> | |
17 | +#include <limits> | |
18 | +#include <vector> | |
19 | +#include <climits> | |
20 | +#include <cstring> | |
21 | +#include <cstdlib> | |
22 | +#include <fstream> | |
23 | +#include <numeric> | |
24 | +#include <sstream> | |
25 | +#include <iostream> | |
26 | +#include <algorithm> | |
27 | +#include <unordered_map> | |
28 | + | |
29 | +using namespace std; | |
30 | + | |
31 | +unsigned long long SumSquareDifference(unsigned long long n){ | |
32 | + unsigned long long sum_qua = 0; | |
33 | + unsigned long long qua_sum = 0; | |
34 | + | |
35 | + sum_qua = (n * (n + 1) * (2*n + 1)) / 6; | |
36 | + qua_sum = n * (n +1 ) / 2; | |
37 | + // for(unsigned long long j = 1; j <= n; j++){ | |
38 | + // sum_qua += j * j; | |
39 | + // qua_sum += j; | |
40 | + // } | |
41 | + return (qua_sum * qua_sum) - sum_qua; | |
42 | +} | |
43 | +int main(){ | |
44 | + unsigned long long t; | |
45 | + cin >> t; | |
46 | + for(unsigned long long i = 0; i < t; i++){ | |
47 | + unsigned long long n; | |
48 | + cin >> n; | |
49 | + cout << SumSquareDifference(n) << endl; | |
50 | + } | |
51 | + | |
52 | + return 0; | |
53 | +} |
NumberTheoryandCombinatorics/SummationOfPrimes/SumPrimes.cpp
View file @
b29dd2c
1 | +// | |
2 | +// Created by ronal on 2/12/2023. | |
3 | +// Problem description link. | |
4 | +// https://www.hackerrank.com/contests/projecteuler/challenges/euler010/problem | |
5 | + | |
6 | +#include <bits/stdc++.h> | |
7 | +#define N 1000000 | |
8 | + | |
9 | +using namespace std; | |
10 | + | |
11 | +bool isPrime(unsigned long long n){ | |
12 | + | |
13 | + if(n == 2 || n == 3){ | |
14 | + return true; | |
15 | + | |
16 | + }else if(n <= 1 || n % 2 == 0 || n % 3 == 0){ | |
17 | + return false; | |
18 | + }else{ | |
19 | + for(int i = 5; i * i <= n; i += 6){ | |
20 | + if(n % i == 0 || n % (i + 2) == 0){ | |
21 | + return false; | |
22 | + } | |
23 | + } | |
24 | + } | |
25 | + return true; | |
26 | +} | |
27 | + | |
28 | +void PrimesNumbers(vector<unsigned long long> &primes, unsigned long long size){ | |
29 | + unsigned long long sum = 0; | |
30 | + for(unsigned long long i = 0; i < primes.size(); i++){ | |
31 | + if( primes[i] <= size){ | |
32 | + sum += primes[i]; | |
33 | + }else{ | |
34 | + break;; | |
35 | + } | |
36 | + } | |
37 | + cout << sum << "\n"; | |
38 | +} | |
39 | + | |
40 | +int main(){ | |
41 | + ios_base::sync_with_stdio(false); | |
42 | + cin.tie(NULL); | |
43 | + vector<unsigned long long> primes; | |
44 | + unsigned long long t, n; | |
45 | + for(unsigned long long i = 0; i <= N; i++){ | |
46 | + if(isPrime(i)){ | |
47 | + primes.push_back(i); | |
48 | + } | |
49 | + } | |
50 | + cin >> t; | |
51 | + for(unsigned long long a0 = 0; a0 < t; a0++){ | |
52 | + cin >> n; | |
53 | + PrimesNumbers(primes, n); | |
54 | + } | |
55 | + return 0; | |
56 | +} |