From bdf01bd761d9f236d8e39d292ea737c40eb88be2 Mon Sep 17 00:00:00 2001 From: "jakob.schratter" Date: Wed, 22 Oct 2025 15:41:37 +0200 Subject: [PATCH] Upload files to "ex1F_goldbachs_conjecture" --- ex1F_goldbachs_conjecture/goldbach.cpp | 42 ++++++++++++++ ex1F_goldbachs_conjecture/goldbach.h | 21 +++++++ ex1F_goldbachs_conjecture/main.cpp | 37 ++++++++++++ ex1F_goldbachs_conjecture/mayer_primes.h | 73 ++++++++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 ex1F_goldbachs_conjecture/goldbach.cpp create mode 100644 ex1F_goldbachs_conjecture/goldbach.h create mode 100644 ex1F_goldbachs_conjecture/main.cpp create mode 100644 ex1F_goldbachs_conjecture/mayer_primes.h diff --git a/ex1F_goldbachs_conjecture/goldbach.cpp b/ex1F_goldbachs_conjecture/goldbach.cpp new file mode 100644 index 0000000..0c7de59 --- /dev/null +++ b/ex1F_goldbachs_conjecture/goldbach.cpp @@ -0,0 +1,42 @@ +#include "goldbach.h" + +size_t single_goldbach(size_t k) +{ + const std::vector 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 count_goldbach(size_t n) +{ + const std::vector relevant_primes = get_primes(n); + size_t m = relevant_primes.size(); + + std::vector 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; +} diff --git a/ex1F_goldbachs_conjecture/goldbach.h b/ex1F_goldbachs_conjecture/goldbach.h new file mode 100644 index 0000000..5794ad4 --- /dev/null +++ b/ex1F_goldbachs_conjecture/goldbach.h @@ -0,0 +1,21 @@ +#pragma once +#include "mayer_primes.h" +#include +#include +#include +#include + + +/** + 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 count_goldbach(size_t n); \ No newline at end of file diff --git a/ex1F_goldbachs_conjecture/main.cpp b/ex1F_goldbachs_conjecture/main.cpp new file mode 100644 index 0000000..e85654f --- /dev/null +++ b/ex1F_goldbachs_conjecture/main.cpp @@ -0,0 +1,37 @@ +#include "../utils/timing.h" +#include "goldbach.h" +#include +#include +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; +} \ No newline at end of file diff --git a/ex1F_goldbachs_conjecture/mayer_primes.h b/ex1F_goldbachs_conjecture/mayer_primes.h new file mode 100644 index 0000000..6c6dc23 --- /dev/null +++ b/ex1F_goldbachs_conjecture/mayer_primes.h @@ -0,0 +1,73 @@ +#pragma once + +#include //memset +#include +//using namespace std; + +/** \brief Determines all prime numbers in interval [2, @p max]. + * + * The sieve of Eratosthenes is used. + * + * The implementation originates from Florian Mayer. + * + * \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 +std::vector get_primes(T max) +{ + std::vector 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 primes; +// primes = get_primes(10000000); +// // return 0; +// // Print out result. +// vector::iterator it; +// for(it=primes.begin(); it < primes.end(); it++) +// cout << *it << " "; +// +// cout << endl; +// return 0; +//}