r/projecteuler • • Jul 28 '21

Multiplies of 2 vs 3/5 problem 1

0 Upvotes

Hello,

I just completed my first problem. I also proceeded to calculate values for even numbers. The problem statement dictates that the multiplies of 3 and 5 below ten add up to 23 so it made sense when I found my sum to be 233,168 however for even numbers my sum below ten is 20 but I still calculated my even sum to be 249,500. Am I misunderstanding the purpose of summing the multiplies below ten?


r/projecteuler • • Jul 13 '20

How many times can you cross International borders in a straight line through Baarle?

Post image
0 Upvotes

r/projecteuler • • Jun 17 '23

Is there a code editor for Mac that you could recommend. to me?

2 Upvotes

It seems that the current app I'm using (Visual Studio Code) can't process more than 10 digits, which is a bummer because as far as I can tell, some questions require me to process over dozens of integers of numbers so that I can get the answer. Are there any code apps that are more powerful than VSC that Mac users can use? Thanks for answering this.


r/projecteuler • • May 25 '16

Problem 1: Multiples of 3 and 5

Thumbnail
joeysprogrammingblog.com
0 Upvotes

r/projecteuler • • Feb 09 '17

First 55 problems solved in Ruby

1 Upvotes

r/projecteuler • • Jul 03 '20

Project Euler Coding Challenges Week One: Problems 1 - 6

Thumbnail
youtube.com
1 Upvotes

r/projecteuler • • Jul 27 '25

Project Euler HackerRank 188

0 Upvotes

I was working on Project Euler 188 on HackerRank and I’m getting WA/TLE on 3–4 test cases.

My approach is roughly as follows:

Factorize m using Pollard–Rho and Miller–Rabin.

For each prime power p^k, compute the hyper‑exponentiation a^(a^(a…)) mod p^k,

handling separately the case when a is divisible by p and when a and p are coprime (using Euler’s totient and recursive reduction of the exponent modulo φ(p^k)).

Finally, combine residues using CRT.

Below is my implementation:

#include <bits/stdc++.h>

using namespace std;

using u64 = unsigned long long;

using u128 = unsigned __int128;

u64 mul_mod(u64 a, u64 b, u64 m) {

return (u128)a * b % m;

}

u64 pow_mod(u64 base, u64 exp, u64 m) {

u64 result = 1 % m;

base %= m;

while (exp) {

if (exp & 1) result = mul_mod(result, base, m);

base = mul_mod(base, base, m);

exp >>= 1;

}

return result;

}

u64 gcd64(u64 a, u64 b) {

while (b) { u64 r = a % b; a = b; b = r; }

return a;

}

u64 mod_mul64(u64 a, u64 b, u64 m) { return (u128)a * b % m; }

u64 mod_pow64(u64 a, u64 d, u64 m) {

u64 res = 1;

while (d) {

if (d & 1) res = mod_mul64(res, a, m);

a = mod_mul64(a, a, m);

d >>= 1;

}

return res;

}

bool isPrime(u64 n) {

if (n < 2) return false;

for (u64 p: {2ULL,3ULL,5ULL,7ULL,11ULL,13ULL,17ULL,19ULL,23ULL}) {

if (n%p==0) return n==p;

}

u64 d = n - 1, s = 0;

while ((d & 1) == 0) { d >>= 1; s++; }

for (u64 a: {2ULL, 325ULL, 9375ULL, 28178ULL, 450775ULL, 9780504ULL, 1795265022ULL}) {

if (a % n == 0) continue;

u64 x = mod_pow64(a, d, n);

if (x == 1 || x == n-1) continue;

bool composite = true;

for (u64 r = 1; r < s; r++) {

x = mod_mul64(x, x, n);

if (x == n-1) { composite = false; break; }

}

if (composite) return false;

}

return true;

}

u64 pollardRho(u64 n) {

if (n % 2 == 0) return 2;

static std::mt19937_64 gen((u64)chrono::high_resolution_clock::now().time_since_epoch().count());

std::uniform_int_distribution<u64> dist(2, n-2);

while (true) {

u64 x = dist(gen), y = x;

u64 c = dist(gen);

if (c >= n) c %= n;

u64 d = 1;

while (d == 1) {

x = (mod_mul64(x, x, n) + c) % n;

y = (mod_mul64(y, y, n) + c) % n;

y = (mod_mul64(y, y, n) + c) % n;

d = gcd64((x>y? x-y: y-x), n);

if (d == n) break;

}

if (d > 1 && d < n) return d;

}

}

void factorRec(u64 n, map<u64,int> &fac) {

if (n <= 1) return;

if (isPrime(n)) {

fac[n]++;

} else {

u64 d = pollardRho(n);

factorRec(d, fac);

factorRec(n/d, fac);

}

}

__int128 crt_combine(__int128 a, __int128 m, __int128 b, __int128 n) {

__int128 rhs = (b - a) % n;

if (rhs < 0) rhs += n;

__int128 r0 = m, r1 = n;

__int128 s0 = 1, s1 = 0;

__int128 t0 = 0, t1 = 1;

while (r1 != 0) {

__int128 q = r0 / r1;

__int128 rr = r0 - q * r1; r0 = r1; r1 = rr;

__int128 ss = s0 - q * s1; s0 = s1; s1 = ss;

__int128 tt = t0 - q * t1; t0 = t1; t1 = tt;

}

__int128 inv = (s0 % n + n) % n;

__int128 t = (rhs * inv) % n;

if (t < 0) t += n;

__int128 x = a + m * t;

return x;

}

u64 tetration_mod(u64 a, u64 b, u64 m) {

if (m == 1) return 0;

if (b == 0) return 1 % m;

if (b == 1) return a % m;

map<u64,int> fac;

factorRec(m, fac);

__int128 result = 0;

__int128 M = 1;

for (auto &[p, k] : fac) {

u64 pk = 1;

for(int i=0;i<k;i++) pk *= p;

u64 res_mod_pk = 0;

if (a % p == 0) {

u64 apow = 0;

u64 tmp = a;

while (tmp % p == 0) { tmp /= p; apow++; }

u64 need = (k + apow - 1) / apow;

u64 E = a % (need+1);

if (b-1 > 1) {

u64 cur = a;

for (u64 i = 2; i <= b-1; i++) {

if (cur >= need) break;

__int128 pw = 1;

for (u64 j = 0; j < cur; j++) {

pw = pw * a;

if (pw >= (__int128)need) { pw = need; break; }

}

cur = (u64)pw;

}

E = cur;

}

if ((__int128)E * apow >= k) {

res_mod_pk = 0;

} else {

res_mod_pk = 1;

u64 base = a % pk;

u64 exp = E;

while (exp) {

if (exp & 1) res_mod_pk = mul_mod(res_mod_pk, base, pk);

base = mul_mod(base, base, pk);

exp >>= 1;

}

}

}

else {

u64 phi = pk / p * (p - 1);

u64 e = tetration_mod(a, b-1, phi);

if (b > 2) { }

if (e < phi) {

} else {

e = e % phi + phi;

}

res_mod_pk = pow_mod(a, e, pk);

}

result = crt_combine(result, M, res_mod_pk, pk);

M *= pk;

}

return (u64)(result % m);

}

int main(){

ios::sync_with_stdio(false);

cin.tie(NULL);

int Q;

if(!(cin>>Q)) return 0;

while(Q--) {

u64 a,b,m;

cin>>a>>b>>m;

if (m == 1) {

cout<<0<<"\n";

continue;

}

if (a == 0) {

if (b == 1) cout<<0<<"\n";

else cout<<1 % m<<"\n";

continue;

}

if (a == 1) {

cout<<1 % m<<"\n";

continue;

}

u64 ans = tetration_mod(a, b, m);

cout<<ans<<"\n";

}

return 0;

}

I saw that your solution scores a perfect 100.

Could you kindly point out what I’m missing or optimizing incorrectly?

Any guidance would mean a lot!

Thanks a ton in advance.


r/projecteuler • • Apr 29 '14

Problem 24 Solution in Python

0 Upvotes

So I solved problem 24 in python; this is actually a general approach, designed to find any given Lexicographic permutations of the first some number of digits.

I think that's rather cool.

import math

def getNthLexicon(numElements, n): #returns the nth lexicographic permutation of the first numElements numbers

baseArray =  list(xrange(0, numElements))
workingArray = baseArray
permArray = []  #this is the empty ray we will be returning

thisSpace = len(baseArray) - 1   #the name refers to the fact we are going to be using this var to reduce our search space

thisN = n - 1 #we want the nth element, but arrays start their indexes at 0.

for each in xrange(0, numElements):  #do this loop for every element in our final list
	facSpace = math.factorial(thisSpace)
	
	thisElement = math.floor(thisN/facSpace)
	thisElement = int(thisElement) #even though floor returns integers, we need to make this an int to allow thisElement to be allowed in list indexcies.

	permArray.append(workingArray[thisElement])
	workingArray.pop(thisElement)

	thisSpace -= 1 #the working array gets smaller each time we run this function
	thisN -= thisElement * facSpace #this could also have been accomplished wiht some sort of mod function, I feel


return permArray

print getNthLexicon(3, 3) # test case for the example problem print getNthLexicon(3, 6) # test case for the example problem

print getNthLexicon(10, 1000000) #this is our official problem


r/projecteuler • • Aug 19 '15

Help with problem 265 (Binary Circles)

0 Upvotes

So I came across Project Euler and after finding the first problem too easy decided to jump ahead and randomly pick a (for me) hard one out.

Now I've managed to write what I think is a solution by simply brute forcing the problem (solves it for n=3), but it uses too much memory to do n=5...

I'm thinking I could rewrite it and split things up or maybe even switch to C++ (takes quite a while until I get an error atm in Python). But at the same time I can't help but feel there's probably a clever analytical way to solving the problem. Which is basically my question, does one exist and if yes could you give me a hint. :D

Here's my code, with n=5 it seems like already my very first list become way too large. Note I'm new to Python so there are doubtless a lot of things that could be done in a better way, feel free to point out whatever you think deserves a mention.


r/projecteuler • • Sep 13 '14

problem 23 common lisp solution

0 Upvotes

This solution takes under 30 seconds. I tried to make it faster but the attempts (using others' solutions, not necessarily in common lisp) ended up being slower. Any recommendations on how I can speed it up or tidy up the code would be welcomed (something tells me this code can be better but I just can't see it. I tried and it just got uglier).

(defun abundantp (num)
  (< num
     (loop for i from 1 to (floor num 2)
           if (zerop (mod num i))
           sum i))) 

(defun abundants ()
  (loop for i from 12 to 28123
        if (abundantp i)
        collect i))

(defun sum-abundants ()
  (remove-duplicates
    (loop with abun = (abundants)
          for i in abun
          nconc (loop for j in abun
                      when (>= j i)
                      collect (+ i j)))))

(defun problem23 ()
  (loop with abun = (sum-abundants)
        for i from 1 to 28123
        unless (member i abun)
        sum i))

r/projecteuler • • Oct 13 '20

On problems like #13 and #16 They should put a disclaimer saying you shouldn't use BigIntever or WolframAlpha and things like that

4 Upvotes

If you look at the threads so many people miss the point. The point is for you to create an algorithm that can return the number as a string not to use Biginteger or WolfranAlpha, because using BigInteger or WolframAlpha is not a challenge. Or u could create ur own bigIntever datatype.


r/projecteuler • • Oct 05 '25

Solved 961

Post image
28 Upvotes

I removed the hint.


r/projecteuler • • Feb 05 '17

Solutions to the First 50 Problems in Python 3.6

Thumbnail
github.com
3 Upvotes

r/projecteuler • • Mar 20 '23

Thinking about writing a book from my solutions, I'd like some feedback on the concept

3 Upvotes

I've previously done the 100 Project Euler problems challenge and am now thinking of compiling what I've done into a book and publishing it. I would have a template for each problem such as:

  1. Turn the word-based question into the math problem.

  2. Determine what math we'd need to know to solve.

  3. Outline the math in the book.

  4. Write the algorithm.

  5. Refine the algorithm?

  6. Solve the problem.

I might also make groupings of problem, concept, problem, etc., such as making an introductory section to prime numbers instead of introducing it the first time the knowledge is needed during a problem.

Before spending the effort on the book, I'd like to ask:

  1. Has anyone else here read books to help with your own Project Euler problems, either books that are directly about Project Euler or about math in general?

  2. If you were to read this kind of a book, what would you want to see in it?

  3. Would making such a book take away the fun of solving these problems? I'm thinking that I may be able to write it in a way which it doesn't, maybe by slowly introducing things one-by-one to give the reader opportunity to figure out all or most things on their own, or by calling this a "reference" instead of a "how-to". I'd like to hear your opinions on this.


r/projecteuler • • Nov 08 '23

Which intellectual hobby should I choose, Chess or Project Euler?

3 Upvotes

I would like to master something, be good at something intellectual in my life and I want that thing to be fun even in my old age


r/projecteuler • • Dec 11 '25

Project Euler Problem #18i

Thumbnail projecteuler.net
1 Upvotes

I was on my laptop solving Project Euler problems when i randomly came across this question. It appeared as a bonus question after i had completed question #63. I asked Gemini and ChatGPT to solve this but it turn out neither could do it. I am stuck on this problem-i could just skip it entirely but i really want to know the answer so please help me do it!


r/projecteuler • • Oct 06 '20

Calculus related problems?

2 Upvotes

Can someone name me some of the calculus related problems?


r/projecteuler • • May 08 '14

Solutions in JavaScript to #1-41 + a few others

Thumbnail
github.com
2 Upvotes

r/projecteuler • • Jan 17 '17

I created a chrome extension that screen records you talking through project euler problems and uploads video to youtube.

2 Upvotes

download url

I enjoy solving project euler problems and when my mind is fresh after solving it, I like to talk about my thought process and solution and save it to youtube. Was wondering if any ya'll would enjoy this function as well. It is in its first renditions, so feedback is welcome. And I apologize if it is buggy. -thanks


r/projecteuler • • Jul 18 '13

Whenever people say it took them forever to solve a problem, and I solved it in seconds.

Thumbnail
livememe.com
14 Upvotes

r/projecteuler • • Oct 19 '21

A hint for anyone stumped by problem 768 (chandelier)

3 Upvotes

I was spinning my wheels against this problem last night, because the code I created answered both examples correctly, but gave the wrong answer to the problem inputs.

After hours of scratching my head, an abstract hint to find the combinations I was missing and have since found is: house-shape


r/projecteuler • • Nov 03 '16

Making a Project Euler API!

3 Upvotes

Hey everyone! I decided to make an unofficial REST API for Project Euler (https://github.com/MainShayne233/project_euler_api/). It is currently running, useable, but incomplete! I have no affiliation with Project Euler, and this application does not have access to their backend. Instead, I have just been harvesting solutions, and would love for anyone to contribute solutions to make it more complete. Instructions to do so are on the Github page (simple pull request). The app simply lets you know if your solution is correct or not, but I would love to hear/receive pull requests for new features, and I hope others find it useful/enjoyable!


r/projecteuler • • Sep 16 '19

Problem Descriptions....

0 Upvotes

Is it just me, or is it that the problem descriptions are intentionally obfuscated? I think there are often times that they can be worded in a better way. At the same time, maybe that's part of the challenge. Still, kind of annoying.


r/projecteuler • • Sep 13 '19

Exercise 50

0 Upvotes

The prime 41, can be written as the sum of six consecutive primes:

41 = 2 + 3 + 5 + 7 + 11 + 13

This is the longest sum of consecutive primes that adds to a prime below one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?

I think the statement has an error:

This is okay:

"41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred."

but this...

"The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953."

The 21 terms: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89. And that is equals to 963.

And is not a prime number... but 953 is the prime number more close.

I misunderstood the excercise?