diff --git a/a074.cpp b/a074.cpp new file mode 100644 index 0000000..ac80f61 --- /dev/null +++ b/a074.cpp @@ -0,0 +1,68 @@ +#include +#include +#include + +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 elapsed_seconds = end - start; + cout << "cycles took: " << elapsed_seconds.count() << endl; + + cout << "numbers with 60 cycles: " << sixties; + return 0; +} diff --git a/a075.cpp b/a075.cpp new file mode 100644 index 0000000..c5a8d10 --- /dev/null +++ b/a075.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#define lmax 1500000 + +using namespace std; +typedef unsigned char byte; + +inline int isqrt(int n) { + return (int)sqrt((float)n); +} + +template +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 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; +} \ No newline at end of file diff --git a/a076.cpp b/a076.cpp new file mode 100644 index 0000000..fd10bb1 --- /dev/null +++ b/a076.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +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 elapsed_seconds = end-start; + cout << "problem took: " << elapsed_seconds.count() << endl; + + cout << m[100][99]; + + return 0; +} \ No newline at end of file diff --git a/a091.cpp b/a091.cpp new file mode 100644 index 0000000..f437d1b --- /dev/null +++ b/a091.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#define sq 3 + +using namespace std; +typedef unsigned char byte; + +template +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 elapsed_seconds = end-start; + cout << "solving took: " << elapsed_seconds.count() << endl; + + cout << "triangles: " << count; + return 0; +} diff --git a/a094.cpp b/a094.cpp new file mode 100644 index 0000000..4c97ca3 --- /dev/null +++ b/a094.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#define pmax 1000000000ll + +using namespace std; +typedef long long var; + +template 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 elapsed_seconds = end-start; + cout << "solving took: " << elapsed_seconds.count() << endl; + cout << "perimeter count: " << count << endl; + + cout << "perimeter sum: " << perimeterSum; + return 0; +} diff --git a/a206.cpp b/a206.cpp new file mode 100644 index 0000000..f004464 --- /dev/null +++ b/a206.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#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 elapsed_seconds = end-start; + cout << "iterating took: " << elapsed_seconds.count() << endl; + + return 0; +} \ No newline at end of file diff --git a/a233.cpp b/a233.cpp new file mode 100644 index 0000000..f9e575c --- /dev/null +++ b/a233.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +typedef long long int Int; + +using namespace std; + +vector 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 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; +}