Advertisement
Guest User

Prime seieve C++

a guest
Oct 16th, 2024
1,706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | Source Code | 0 0
  1. #include "benchmark.h"
  2. #include <cstddef>
  3. #include <godot_cpp/core/class_db.hpp>
  4. #include <godot_cpp/classes/engine.hpp>
  5. #include <godot_cpp/variant/utility_functions.hpp>
  6.  
  7. using namespace godot;
  8.  
  9. void Benchmark::_bind_methods() {
  10.     ClassDB::bind_method(D_METHOD("get_limit"), &Benchmark::get_limit);
  11.     ClassDB::bind_method(D_METHOD("set_limit", "p_limit"), &Benchmark::set_limit);
  12.     ClassDB::bind_method(D_METHOD("how_many_primes" , "p_limit"), &Benchmark::how_many_primes);
  13.  
  14.     ADD_PROPERTY(PropertyInfo(Variant::INT, "limit"), "set_limit", "get_limit");
  15.    
  16. }
  17.  
  18. Benchmark::Benchmark() {
  19.     // Initialize any variables here.
  20.     limit = 25000;
  21.  
  22. }
  23.  
  24. Benchmark::~Benchmark() {
  25.     // Add your cleanup here.
  26. }
  27.  
  28. void Benchmark::_ready() {
  29.     if(Engine::get_singleton()->is_editor_hint()) { return; }
  30.    
  31.     // how_many_primes(limit);
  32.  
  33.  
  34. }
  35.  
  36. int Benchmark::is_prime(int n) {
  37.     for (int i = 2 ; i <= n/2 ; ++i) {
  38.         if (n % i == 0) { return 0;}
  39.     }
  40.     return 1;
  41. }
  42.  
  43. int Benchmark::how_many_primes(int p_limit) {
  44.     int numPrimes = 0;
  45.     for (int i = 2; i < p_limit; ++i) {
  46.         numPrimes += is_prime(i) ;
  47.     }
  48.     UtilityFunctions::print(numPrimes);
  49.     return numPrimes;
  50. }
  51.  
  52. void Benchmark::set_limit(const int p_limit) {
  53.     limit = p_limit;
  54. }
  55.  
  56. int Benchmark::get_limit() const {
  57.     return limit;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement