C++ solutions 74, 75, 76, 91, 94, 206, 233
This commit is contained in:
		
							parent
							
								
									b81b9e6e5e
								
							
						
					
					
						commit
						8e6b92ea9d
					
				| 
						 | 
				
			
			@ -0,0 +1,68 @@
 | 
			
		|||
#include <iostream>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
int factorials[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
 | 
			
		||||
int cycleLength[2200000];
 | 
			
		||||
 | 
			
		||||
inline int digitFactorial(int a)
 | 
			
		||||
{
 | 
			
		||||
    int sum = 0;
 | 
			
		||||
    while (a > 0)
 | 
			
		||||
    {
 | 
			
		||||
        sum += factorials[a % 10];
 | 
			
		||||
        a /= 10;
 | 
			
		||||
    }
 | 
			
		||||
    return sum;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int cycle(int n, int depth)
 | 
			
		||||
{
 | 
			
		||||
    if (depth > 1000)
 | 
			
		||||
        cout << "{ \"depth\": " << depth << ", \"n\": " << n << " }" << endl;
 | 
			
		||||
    int cl = cycleLength[n];
 | 
			
		||||
    if (cl < 0)
 | 
			
		||||
        return -cl;
 | 
			
		||||
    if (cl == 0)
 | 
			
		||||
    {
 | 
			
		||||
        cl = cycle(digitFactorial(n), depth+1);
 | 
			
		||||
        cycleLength[n] = cl;
 | 
			
		||||
    }
 | 
			
		||||
    return cl+1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    auto start = chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    cycleLength[0] = -1;
 | 
			
		||||
    cycleLength[1] = -1;
 | 
			
		||||
    cycleLength[2] = -1;
 | 
			
		||||
    cycleLength[169] = -3;
 | 
			
		||||
    cycleLength[145] = -1;
 | 
			
		||||
    cycleLength[871] = -2;
 | 
			
		||||
    cycleLength[872] = -2;
 | 
			
		||||
    cycleLength[1454] = -3;
 | 
			
		||||
    cycleLength[40585] = -3;
 | 
			
		||||
    cycleLength[45361] = -2;
 | 
			
		||||
    cycleLength[45362] = -2;
 | 
			
		||||
    cycleLength[363601] = -3;
 | 
			
		||||
 | 
			
		||||
    cout << "cycle length 69: " << cycle(69,0) << endl;
 | 
			
		||||
 | 
			
		||||
    int sixties = 0;
 | 
			
		||||
    for (int n = 0; n < 1000000; n++)
 | 
			
		||||
    {
 | 
			
		||||
        if (cycle(n,0) == 60)
 | 
			
		||||
            sixties++;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto end = std::chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    std::chrono::duration<double> elapsed_seconds = end - start;
 | 
			
		||||
    cout << "cycles took: " << elapsed_seconds.count() << endl;
 | 
			
		||||
 | 
			
		||||
    cout << "numbers with 60 cycles: " << sixties;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,75 @@
 | 
			
		|||
#include <iostream>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
#define lmax 1500000
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
typedef unsigned char byte;
 | 
			
		||||
 | 
			
		||||
inline int isqrt(int n) {
 | 
			
		||||
    return (int)sqrt((float)n);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<class T>
 | 
			
		||||
inline T gcd(T a, T b) {
 | 
			
		||||
    while(b) {
 | 
			
		||||
        auto t = a % b;
 | 
			
		||||
        a = b;
 | 
			
		||||
        b = t;
 | 
			
		||||
    }
 | 
			
		||||
    return a;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    byte arr[lmax+1];
 | 
			
		||||
    auto start = chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i <= lmax; i++)
 | 
			
		||||
        arr[i] = 0;
 | 
			
		||||
 | 
			
		||||
    for (int length = 12; length <= lmax; length += 2)
 | 
			
		||||
    for (int m = isqrt(length)/2+1; m <= isqrt(length/2); m++) {
 | 
			
		||||
        if (length % (2*m) == 0) 
 | 
			
		||||
            {
 | 
			
		||||
                int n = length / (2*m) - m;
 | 
			
		||||
                if (n>0 && n < m && n%2!= m%2 && gcd(m,n) == 1)
 | 
			
		||||
                arr[length]++;
 | 
			
		||||
                }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto middle = std::chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    std::chrono::duration<double> elapsed_seconds = middle-start;
 | 
			
		||||
    cout << "generating primitives took: " << elapsed_seconds.count() << endl;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    for (int length = lmax; length >= 0; length--) {
 | 
			
		||||
        int sqrtlen = isqrt(length);
 | 
			
		||||
        for (int prev_length = 2; prev_length <= sqrtlen; prev_length++)
 | 
			
		||||
            if (length % prev_length == 0) {
 | 
			
		||||
                arr[length] += arr[prev_length];
 | 
			
		||||
                arr[length] += arr[length/prev_length];
 | 
			
		||||
                if (arr[length] > 1)
 | 
			
		||||
                    break;
 | 
			
		||||
            }
 | 
			
		||||
    }
 | 
			
		||||
    auto end = std::chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    elapsed_seconds = end-middle;
 | 
			
		||||
    cout << "adding multiples took: " << elapsed_seconds.count() << endl;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    int singles = 0;
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i <= lmax; i++)
 | 
			
		||||
        if (arr[i] == 1)
 | 
			
		||||
            singles++;
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i <= 60; i++)
 | 
			
		||||
        if (arr[i] == 1)
 | 
			
		||||
            cout << i << ", ";
 | 
			
		||||
    cout << endl;
 | 
			
		||||
 | 
			
		||||
    cout << singles;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,35 @@
 | 
			
		|||
#include <iostream>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    int m[101][101];
 | 
			
		||||
    auto start = chrono::high_resolution_clock::now();
 | 
			
		||||
 | 
			
		||||
    for (int n = 1; n <= 100; n++)
 | 
			
		||||
        m[1][n] = 1;
 | 
			
		||||
 | 
			
		||||
    for (int t = 1; t <= 100; t++)
 | 
			
		||||
        m[t][1] = 1;
 | 
			
		||||
 | 
			
		||||
    for (int t = 2; t <= 100; t++)
 | 
			
		||||
    for (int n = 2; n <= 100; n++) {
 | 
			
		||||
        if (n >  t)
 | 
			
		||||
            m[t][n] = m[t][n-1];
 | 
			
		||||
        if (n == t)
 | 
			
		||||
            m[t][n] = m[t][n-1]+1;
 | 
			
		||||
        if (n <  t)
 | 
			
		||||
            m[t][n] = m[t][n-1]+m[t-n][n];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto end = std::chrono::high_resolution_clock::now();
 | 
			
		||||
 | 
			
		||||
    std::chrono::duration<double> elapsed_seconds = end-start;
 | 
			
		||||
    cout << "problem took: " << elapsed_seconds.count() << endl;
 | 
			
		||||
 | 
			
		||||
    cout << m[100][99];
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,43 @@
 | 
			
		|||
#include <iostream>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
#define sq 3
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
typedef unsigned char byte;
 | 
			
		||||
 | 
			
		||||
template<class T>
 | 
			
		||||
inline T gcd(T a, T b) {
 | 
			
		||||
    while(b) {
 | 
			
		||||
        auto t = a % b;
 | 
			
		||||
        a = b;
 | 
			
		||||
        b = t;
 | 
			
		||||
    }
 | 
			
		||||
    return a;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    auto start = chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    //triangles for which either (x1,y1) == (0,0)
 | 
			
		||||
    //or x1==0,y1!=0 or x1!=0,y1==0
 | 
			
		||||
    long count = 3*sq*sq;
 | 
			
		||||
 | 
			
		||||
    for (int vx = 1; vx <= sq; vx++)
 | 
			
		||||
    for (int vy = 1; vy <= sq; vy++) {
 | 
			
		||||
        int dx = -vy/gcd(vx,vy);
 | 
			
		||||
        int dy = vx/gcd(vx,vy);
 | 
			
		||||
        for (int d = 1; vx+dx*d >= 0 && vy+dy*d <= sq; d++)
 | 
			
		||||
            count++;
 | 
			
		||||
        for (int d = -1; vx+dx*d <= sq && vy+dy*d >= 0; d--)
 | 
			
		||||
            count++;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto end = std::chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    std::chrono::duration<double> elapsed_seconds = end-start;
 | 
			
		||||
    cout << "solving took: " << elapsed_seconds.count() << endl;
 | 
			
		||||
 | 
			
		||||
    cout << "triangles: " << count;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,46 @@
 | 
			
		|||
#include <iostream>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
#define pmax 1000000000ll
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
typedef long long var;
 | 
			
		||||
 | 
			
		||||
template<typename T> inline bool isSquare(T val) {
 | 
			
		||||
    T root = (T)sqrt((double)val);
 | 
			
		||||
    return root*root == val;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    auto start = chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    var perimeterSum = 0;
 | 
			
		||||
    var count = 0;
 | 
			
		||||
 | 
			
		||||
    for (var equalSide = 3; equalSide <= pmax/3+1; equalSide += 2) {
 | 
			
		||||
        var otherSide = equalSide-1;
 | 
			
		||||
        var s = equalSide + otherSide/2;
 | 
			
		||||
        if (isSquare(s*(s-otherSide)) && 2*s <= pmax) {
 | 
			
		||||
            perimeterSum += 2*s;
 | 
			
		||||
            count++;
 | 
			
		||||
            cout << "(" << equalSide << "," << equalSide << "," << otherSide << ")" << endl;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        otherSide = equalSide+1;
 | 
			
		||||
        s = equalSide + otherSide/2;
 | 
			
		||||
        if (isSquare(s*(s-otherSide)) && 2*s <= pmax) {
 | 
			
		||||
            perimeterSum += 2*s;
 | 
			
		||||
            count++;
 | 
			
		||||
            cout << "(" << equalSide << "," << equalSide << "," << otherSide << ")" << endl;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto end = std::chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    std::chrono::duration<double> elapsed_seconds = end-start;
 | 
			
		||||
    cout << "solving took: " << elapsed_seconds.count() << endl;
 | 
			
		||||
    cout << "perimeter count: " << count << endl;
 | 
			
		||||
 | 
			
		||||
    cout << "perimeter sum: " << perimeterSum;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,33 @@
 | 
			
		|||
#include <iostream>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
#define lmax 1500000
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
typedef unsigned char byte;
 | 
			
		||||
 | 
			
		||||
inline int isqrt(int n) {
 | 
			
		||||
    return (int)sqrt((float)n);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline uint64_t isqrt(uint64_t n) {
 | 
			
		||||
    return (uint64_t)sqrt((double)n);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    auto start = chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    for (uint64_t i = isqrt(10203040506070809ull); i <= isqrt(19293949596979899ull); i++) {
 | 
			
		||||
        uint64_t square = i*i;
 | 
			
		||||
 | 
			
		||||
        if (square % 10ull == 9 && (square/100ull) % 10ull == 8 && (square/10000ull) % 10ull == 7 && (square/1000000ull) % 10ull == 6 && (square/100000000ull) % 10ull == 5 && (square/10000000000ull) % 10ull == 4 && (square/1000000000000ull) % 10ull == 3 && (square/100000000000000ull) % 10ull == 2 && (square/10000000000000000ull) == 1)
 | 
			
		||||
            cout << "found " << i << " ^2 = " << square << endl;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    auto end = std::chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    std::chrono::duration<double> elapsed_seconds = end-start;
 | 
			
		||||
    cout << "iterating took: " << elapsed_seconds.count() << endl;
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,87 @@
 | 
			
		|||
#include <iostream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
typedef long long int Int;
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
vector<Int> primes;
 | 
			
		||||
 | 
			
		||||
void generatePrimes() {
 | 
			
		||||
    primes.push_back(2);
 | 
			
		||||
    primes.push_back(3);
 | 
			
		||||
    for (int i = 5; i < 1000000; i += 2) {
 | 
			
		||||
        bool isPrime = true;
 | 
			
		||||
        for (auto p : primes) {
 | 
			
		||||
            if ((int)p*(int)p > i)
 | 
			
		||||
                break;
 | 
			
		||||
            if (i % (int)p == 0) {
 | 
			
		||||
                isPrime = false;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (isPrime)
 | 
			
		||||
            primes.push_back((Int)i);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool check420LatticePoints(Int n) {
 | 
			
		||||
    int latticePoints = 1;
 | 
			
		||||
 | 
			
		||||
    for (auto p : primes) {
 | 
			
		||||
        if (p*p > n)
 | 
			
		||||
            p = n;
 | 
			
		||||
 | 
			
		||||
        int multiplicity = 0;
 | 
			
		||||
 | 
			
		||||
        while (n % p == 0) {
 | 
			
		||||
            multiplicity++;
 | 
			
		||||
            n /= p;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (multiplicity > 0 && p % 4 == 1) {
 | 
			
		||||
            latticePoints *= 2*multiplicity+1;
 | 
			
		||||
 | 
			
		||||
            if (latticePoints == 105)
 | 
			
		||||
                return true;
 | 
			
		||||
 | 
			
		||||
            if (105 % latticePoints != 0)
 | 
			
		||||
                return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (n==1)
 | 
			
		||||
            return false;
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    Int max;
 | 
			
		||||
    cout << "N = ";
 | 
			
		||||
    cin >> max;
 | 
			
		||||
 | 
			
		||||
    auto start = chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    generatePrimes();
 | 
			
		||||
 | 
			
		||||
    auto middle = std::chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    std::chrono::duration<double> elapsed_seconds = middle-start;
 | 
			
		||||
    cout << "generating primes took: " << elapsed_seconds.count() << endl;
 | 
			
		||||
 | 
			
		||||
    Int count = 0, sum = 0;
 | 
			
		||||
    for (Int n = 10; n < max; n++) {
 | 
			
		||||
        if (check420LatticePoints(n)) {
 | 
			
		||||
            sum += n;
 | 
			
		||||
            count++;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    auto end = std::chrono::system_clock::now();
 | 
			
		||||
 | 
			
		||||
    elapsed_seconds = end-middle;
 | 
			
		||||
    cout << "finding 420s took: " << elapsed_seconds.count() << endl << endl;
 | 
			
		||||
 | 
			
		||||
    cout << "sum: " << sum << ", total: " << count;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue