#include <string> #include <string_view> #include <vector> #include <numeric> #include <sstream> std::string Jointup(const std::vector<std::string> &name , const std::string_view delim = " , ") { if (name.empty()) return ""; std::stringstream ss; ss << name.front(); std::for_each(std::next(name.cbegin()), name.cend(), [&ss, delim](auto const& s) { ss << delim << s; }); return ss.str(); }
- #include <string>
- #include <string_view>
- #include <vector>
- #include <numeric>
- #include <sstream>
std::string Jointup(const std::vector<std::string> &name , const std::string_view delim = " , "){if (name.empty()) return "";return std::accumulate(std::next(name.begin()),name.end(),name.front(),[&](auto &s, auto &n){return s.append(delim).append(n);});- std::string Jointup(const std::vector<std::string> &name , const std::string_view delim = " , ")
- {
- if (name.empty()) return "";
- std::stringstream ss;
- ss << name.front();
- std::for_each(std::next(name.cbegin()), name.cend(), [&ss, delim](auto const& s) { ss << delim << s; });
- return ss.str();
- }
calculated in square grid, converges faster but lose precision
#include <cstdlib> #include <cmath> long double pi_estimate(long long n, unsigned int seed = 0) { srand(seed); long long n1 = sqrt(n); long long inside = 0; // calculated in grid, converges faster but lose precision for (long long i = 0; i < n1; i++) for (long long j = 0; j < n1; j++) { double x = 2 * ((i + rand() / (RAND_MAX + 1.0)) / n1) - 1; double y = 2 * ((j + rand() / (RAND_MAX + 1.0)) / n1) - 1; if (x * x + y * y < 1.) inside++; } printf("%.17g\n", 4. * inside / (n1 * n1)); return 4. * inside / (n1 * n1); }
#include <random>- #include <cstdlib>
- #include <cmath>
std::uniform_real_distribution<long double> dis(0.,1.); // Default is doublestd::random_device rd;std::mt19937 gen(rd());long double pi_estimate(long long n, int seed=0) {gen.seed(seed);long long inside=0;for (auto i=0; i!=n; ++i) if (std::pow(dis(gen),2)+std::pow(dis(gen),2) < 1.) ++inside;return (4.*inside)/n;- long double pi_estimate(long long n, unsigned int seed = 0)
- {
- srand(seed);
- long long n1 = sqrt(n);
- long long inside = 0;
- // calculated in grid, converges faster but lose precision
- for (long long i = 0; i < n1; i++)
- for (long long j = 0; j < n1; j++)
- {
- double x = 2 * ((i + rand() / (RAND_MAX + 1.0)) / n1) - 1;
- double y = 2 * ((j + rand() / (RAND_MAX + 1.0)) / n1) - 1;
- if (x * x + y * y < 1.) inside++;
- }
- printf("%.17g\n", 4. * inside / (n1 * n1));
- return 4. * inside / (n1 * n1);
- }
// TODO: Replace examples and use TDD development by writing your own tests #include <limits> #include <iostream> #include <iomanip> bool AreEqual(long double v1, long double v2, long double delta) {return std::abs(v2-v1)<=delta;} Describe(BASE) { It(Basics) { Assert::That(AreEqual(pi_estimate(1000,42), 3.130,0.001),Equals(true)); Assert::That(AreEqual(pi_estimate(1000),3.1426,0.001),Equals(true)); Assert::That(AreEqual(pi_estimate(1'000'000),3.141556,0.00001),Equals(true)); Assert::That(AreEqual(pi_estimate(1'000'000),3.141556,1e-18),Equals(true)); Assert::That(AreEqual(pi_estimate(10'000'000,123456789),3.1416,0.01),Equals(true)); Assert::That(AreEqual(pi_estimate(1'000'000),3.14,0.001),Equals(false)); } };
- // TODO: Replace examples and use TDD development by writing your own tests
- #include <limits>
- #include <iostream>
- #include <iomanip>
- bool AreEqual(long double v1, long double v2, long double delta) {return std::abs(v2-v1)<=delta;}
- Describe(BASE)
- {
- It(Basics)
- {
Assert::That(AreEqual(pi_estimate(1000,42), 3.176,0.001),Equals(true));Assert::That(AreEqual(pi_estimate(1000),3.088,0.001),Equals(true));Assert::That(AreEqual(pi_estimate(1'000'000),3.13758,0.00001),Equals(true));Assert::That(AreEqual(pi_estimate(1'000'000),3.13758,1e-18),Equals(true));Assert::That(AreEqual(pi_estimate(10'000'000,123456789),3.1417696,1e-20),Equals(true));- Assert::That(AreEqual(pi_estimate(1000,42), 3.130,0.001),Equals(true));
- Assert::That(AreEqual(pi_estimate(1000),3.1426,0.001),Equals(true));
- Assert::That(AreEqual(pi_estimate(1'000'000),3.141556,0.00001),Equals(true));
- Assert::That(AreEqual(pi_estimate(1'000'000),3.141556,1e-18),Equals(true));
- Assert::That(AreEqual(pi_estimate(10'000'000,123456789),3.1416,0.01),Equals(true));
- Assert::That(AreEqual(pi_estimate(1'000'000),3.14,0.001),Equals(false));
- }
- };
def prime_factorization(n): factors = [] cnt = 0 while n % 2 == 0: cnt += 1; n //= 2 if cnt: factors.append((2, cnt)) cnt = 0 while n % 3 == 0: cnt += 1; n //= 3 if cnt: factors.append((3, cnt)) i = 5 while i * i <= n: cnt = 0 while n % i == 0: cnt += 1; n //= i if cnt: factors.append((i, cnt)) cnt = 0 i += 2 while n % i == 0: cnt += 1; n //= i if cnt: factors.append((i, cnt)) i += 4 if n > 1: factors.append((n, 1)) return factors
def prime_factorization(input_number):- def prime_factorization(n):
- factors = []
for i in range(2, int(input_number**0.5) + 1):while input_number % i == 0:factors.append(i)input_number //= iif input_number > 1:factors.append(input_number)from collections import Counterreturn sorted(Counter(factors).items())- cnt = 0
- while n % 2 == 0: cnt += 1; n //= 2
- if cnt: factors.append((2, cnt))
- cnt = 0
- while n % 3 == 0: cnt += 1; n //= 3
- if cnt: factors.append((3, cnt))
- i = 5
- while i * i <= n:
- cnt = 0
- while n % i == 0: cnt += 1; n //= i
- if cnt: factors.append((i, cnt))
- cnt = 0
- i += 2
- while n % i == 0: cnt += 1; n //= i
- if cnt: factors.append((i, cnt))
- i += 4
- if n > 1: factors.append((n, 1))
- return factors