goldbachs_conjecture.cpp
This commit is contained in:
79
puzzles/interviews/training/goldbachs_conjecture.cpp
Normal file
79
puzzles/interviews/training/goldbachs_conjecture.cpp
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
#include <vector>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <iostream>
|
||||||
|
#include <cmath>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
static std::vector<int> GetPrimes(const size_t n) {
|
||||||
|
std::vector<int> primes;
|
||||||
|
std::vector<int> lp(n + 1, 0);
|
||||||
|
|
||||||
|
for (auto i = 2u; i <= n; ++i) {
|
||||||
|
if (lp[i] == 0) {
|
||||||
|
lp[i] = i;
|
||||||
|
primes.push_back(i);
|
||||||
|
}
|
||||||
|
for (auto j = 0u; j < primes.size() && primes[j] <= lp[i] && i * primes[j] <= n; ++j)
|
||||||
|
lp[i * primes[j]] = primes[j];
|
||||||
|
}
|
||||||
|
return primes;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool is_prime( int n, const vector<int>& primes ){
|
||||||
|
int sqrt_n = floor(sqrt(double(n)));
|
||||||
|
for ( int i = 0; i < primes.size() && primes[i] <= sqrt_n; ++i ){
|
||||||
|
if ( n % primes[i] == 0 ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<int> primesum(int A) {
|
||||||
|
|
||||||
|
vector<int> primes;
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
for ( int i = 2; i < A; ++i){
|
||||||
|
if ( is_prime(i,primes) ){
|
||||||
|
primes.push_back(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
primes = GetPrimes(A);
|
||||||
|
|
||||||
|
#if 1
|
||||||
|
unordered_set<int> primes_set(primes.begin(),primes.end());
|
||||||
|
for ( int i = 0; i < primes.size(); ++i ){
|
||||||
|
if ( primes_set.find(A-primes[i]) != primes_set.end() ) {
|
||||||
|
return {primes[i], A-primes[i]};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
for ( int i = 0; i < primes.size(); ++i ) {
|
||||||
|
auto it = std::lower_bound(primes.begin(), primes.end(), A - primes[i]);
|
||||||
|
if ( it != primes.end() && *it == A-primes[i] ) {
|
||||||
|
return {primes[i], A-primes[i]};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
void solve_and_print(int A){
|
||||||
|
auto r = primesum(A);
|
||||||
|
std::cout << r[0] << ' ' << r[1] << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
solve_and_print(4);
|
||||||
|
solve_and_print(10);
|
||||||
|
|
||||||
|
solve_and_print(16777214);
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#
|
|
||||||
# Prints all permutations.
|
|
||||||
#
|
|
||||||
def permutation(s, b, e):
|
def permutation(s, b, e):
|
||||||
|
"""
|
||||||
|
Prints all permutations.
|
||||||
|
"""
|
||||||
if b+1 == e:
|
if b+1 == e:
|
||||||
print(s)
|
print(s)
|
||||||
else:
|
else:
|
||||||
@@ -16,12 +16,13 @@ def permutation(s, b, e):
|
|||||||
s[b] = s[e-1]
|
s[b] = s[e-1]
|
||||||
s[e-1] = c
|
s[e-1] = c
|
||||||
|
|
||||||
#
|
|
||||||
# Prints all permutations. But in addition doesn't print
|
|
||||||
# multiple instances of same value.
|
|
||||||
# e.g. abb bab bba ( doesn't print two abb )
|
|
||||||
#
|
|
||||||
def permutation2(s):
|
def permutation2(s):
|
||||||
|
"""
|
||||||
|
Prints all permutations. But in addition doesn't print
|
||||||
|
multiple instances of same value.
|
||||||
|
e.g. abb bab bba ( doesn't print two abb )
|
||||||
|
"""
|
||||||
print(s)
|
print(s)
|
||||||
l = len(s)
|
l = len(s)
|
||||||
|
|
||||||
@@ -41,6 +42,7 @@ def permutation2(s):
|
|||||||
if i == 0:
|
if i == 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
s = ['a', 'b', 'c', 'd', 'e', 'e']
|
s = ['a', 'b', 'c', 'd', 'e', 'e']
|
||||||
permutation(s, 0, len(s))
|
permutation(s, 0, len(s))
|
||||||
|
|||||||
Reference in New Issue
Block a user