Ad
Code
Diff
  • #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

Code
Diff
  • #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 double
    • std::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);
    • }
Code
Diff
  • 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 //= i
    • if input_number > 1:
    • factors.append(input_number)
    • from collections import Counter
    • return 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