Upload files to "ex1F_goldbachs_conjecture"
This commit is contained in:
parent
2ac165d667
commit
bdf01bd761
4 changed files with 173 additions and 0 deletions
42
ex1F_goldbachs_conjecture/goldbach.cpp
Normal file
42
ex1F_goldbachs_conjecture/goldbach.cpp
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#include "goldbach.h"
|
||||
|
||||
size_t single_goldbach(size_t k)
|
||||
{
|
||||
const std::vector<size_t> relevant_primes = get_primes(k);
|
||||
size_t m = relevant_primes.size();
|
||||
|
||||
size_t counter = 0;
|
||||
|
||||
for(size_t i = 0; i < m; ++i)
|
||||
{
|
||||
for(size_t j = i; j < m; ++j)
|
||||
{
|
||||
if(relevant_primes[i] + relevant_primes[j] == k)
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return counter;
|
||||
}
|
||||
|
||||
|
||||
std::vector<size_t> count_goldbach(size_t n)
|
||||
{
|
||||
const std::vector<size_t> relevant_primes = get_primes(n);
|
||||
size_t m = relevant_primes.size();
|
||||
|
||||
std::vector<size_t> counter_vector(n + 1, 0);
|
||||
|
||||
|
||||
for(size_t i = 0; i < m; ++i)
|
||||
{
|
||||
for(size_t j = i; j < m; ++j)
|
||||
{
|
||||
size_t sum = relevant_primes[i] + relevant_primes[j];
|
||||
if(sum <= n)
|
||||
++counter_vector[relevant_primes[i] + relevant_primes[j]];
|
||||
}
|
||||
}
|
||||
|
||||
return counter_vector;
|
||||
}
|
||||
21
ex1F_goldbachs_conjecture/goldbach.h
Normal file
21
ex1F_goldbachs_conjecture/goldbach.h
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#pragma once
|
||||
#include "mayer_primes.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
/**
|
||||
This function returns the number of possible decompositions of an integer into a sum of two prime numbers.
|
||||
@param[in] k first integer
|
||||
@param[out] count number of decompositions
|
||||
*/
|
||||
size_t single_goldbach(size_t k);
|
||||
|
||||
/**
|
||||
This function returns the number of possible decompositions into a sum of two prime numbers of all even integers in the interval [4,n].
|
||||
@param[in] n upper integer bound
|
||||
@param[out] count_vector vector containing the number of decompositions for a natural number the corresponding index
|
||||
*/
|
||||
std::vector<size_t> count_goldbach(size_t n);
|
||||
37
ex1F_goldbachs_conjecture/main.cpp
Normal file
37
ex1F_goldbachs_conjecture/main.cpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include "../utils/timing.h"
|
||||
#include "goldbach.h"
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Check: 694 has "<< single_goldbach(694) << " decompositions." << endl << "----------------------------------------" << endl;
|
||||
|
||||
|
||||
for(size_t n : {10000, 100000, 400000, 1000000, 2000000})
|
||||
{
|
||||
tic();
|
||||
|
||||
auto goldbach_vector = count_goldbach(n);
|
||||
|
||||
auto max_it = max_element(goldbach_vector.begin(), goldbach_vector.end());
|
||||
size_t max_number = distance(goldbach_vector.begin(), max_it);
|
||||
|
||||
double time = toc();
|
||||
|
||||
cout << "The number " << max_number << " has " << *max_it << " decompositions. Duration: " << time << endl;
|
||||
}
|
||||
|
||||
/*
|
||||
The number 9240 has 329 decompositions. Duration: 0.00572876
|
||||
The number 99330 has 2168 decompositions. Duration: 0.3342
|
||||
The number 390390 has 7094 decompositions. Duration: 4.23734
|
||||
The number 990990 has 15594 decompositions. Duration: 29.5817
|
||||
The number 1981980 has 27988 decompositions. Duration: 135.985
|
||||
*/
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
73
ex1F_goldbachs_conjecture/mayer_primes.h
Normal file
73
ex1F_goldbachs_conjecture/mayer_primes.h
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstring> //memset
|
||||
#include <vector>
|
||||
//using namespace std;
|
||||
|
||||
/** \brief Determines all prime numbers in interval [2, @p max].
|
||||
*
|
||||
* The sieve of Eratosthenes is used.
|
||||
*
|
||||
* The implementation originates from <a href="http://code.activestate.com/recipes/576559-fast-prime-generator/">Florian Mayer</a>.
|
||||
*
|
||||
* \param[in] max end of interval for the prime number search.
|
||||
* \return vector of prime numbers @f$2,3,5, ..., p<=max @f$.
|
||||
*
|
||||
* \copyright
|
||||
* Copyright (c) 2008 Florian Mayer (adapted by Gundolf Haase 2018)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
template <class T>
|
||||
std::vector<T> get_primes(T max)
|
||||
{
|
||||
std::vector<T> primes;
|
||||
char *sieve;
|
||||
sieve = new char[max / 8 + 1];
|
||||
// Fill sieve with 1
|
||||
memset(sieve, 0xFF, (max / 8 + 1) * sizeof(char));
|
||||
for (T x = 2; x <= max; x++)
|
||||
{
|
||||
if (sieve[x / 8] & (0x01 << (x % 8))) {
|
||||
primes.push_back(x);
|
||||
// Is prime. Mark multiplicates.
|
||||
for (T j = 2 * x; j <= max; j += x)
|
||||
{
|
||||
sieve[j / 8] &= ~(0x01 << (j % 8));
|
||||
}
|
||||
}
|
||||
}
|
||||
delete[] sieve;
|
||||
return primes;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------
|
||||
//int main() // by Florian Mayer
|
||||
//{g++ -O3 -std=c++14 -fopenmp main.cpp && ./a.out
|
||||
// vector<unsigned long> primes;
|
||||
// primes = get_primes(10000000);
|
||||
// // return 0;
|
||||
// // Print out result.
|
||||
// vector<unsigned long>::iterator it;
|
||||
// for(it=primes.begin(); it < primes.end(); it++)
|
||||
// cout << *it << " ";
|
||||
//
|
||||
// cout << endl;
|
||||
// return 0;
|
||||
//}
|
||||
Loading…
Add table
Add a link
Reference in a new issue