77 votes
Accepted

Project Euler #7 10001st prime

bool isPrime = true; for (int j = 2; j < i / 2; j++) { if (i%j != 0) continue; isPrime = false; break; } if (!isPrime) continue; This is one of the ...
user avatar
  • 25.2k
75 votes
Accepted

100 gunmen in a circle kill next person

1. Encapsulate Writing code at the top level of a module makes it hard to test the code and hard to measure its performance. It's best to encapsulate code in a function. Accordingly, I'd write: <...
user avatar
  • 49.2k
42 votes
Accepted

Project Euler Problem 45

Arithmetic Project Euler questions are meant to educate you about both mathematics and programming. It would be a good idea to understand what these triangular, pentagonal, and hexagonal numbers ...
user avatar
41 votes

Writing nested for loops to produce certain output

You're not breaking up the patterns in the most logical way. Each line just consists of repeating blocks of three characters. ...
user avatar
33 votes
Accepted

Determining if the kangaroos will land in the same position

Closed form solution A better approach is to calculate the intercept point directly and then check whether or not it is an integer. The time of intercept is: $$t = \frac{x_2 - x_1}{v_1 - v_2}$$ ...
user avatar
  • 2,242
33 votes
Accepted

Finding pairs of Amicable Numbers, up to 10000

printf("|---------- PROGRAM FOR AMICABLE NUMBERS.----------|"); This kind of banner message makes it harder to use the output of your program in a ...
user avatar
  • 57.9k
32 votes

Beehive numbers - using goto in C++

Your use of goto is wholly unjustified, not just because goto is taboo, but because your code has flow-of-control that is hard ...
user avatar
32 votes
Accepted

Project Euler Problem #4 - Palindromic number

The other answers are all giving you different ways to reorganize your loops. They're all not bad in their own way, but the better solution in C# is to not write any loops in the first place. Your ...
user avatar
  • 14.6k
32 votes
Accepted

Checking for balanced brackets in Python

Review Your implementation is quite good, but ofcourse there are always some things that can be improved upon. Use a if __name__ == '__main__' guard, see what ...
user avatar
  • 11.4k
32 votes
Accepted

Project Euler 001 in C++

constexpr everything Apart from the #define, your code is quite good. But you miss on a really simple optimization by not using ...
user avatar
  • 5,687
30 votes
Accepted

Sieve of Sundaram for Project Euler 7: Python implementation slower than C++ and R

TL;DR: Just use PyPy; it gets you to about 10x the time of C++. If you really want to use CPython, a lot of clever optimizations (not algorithm changes) gets you as fast as PyPy and then using Numpy ...
user avatar
  • 9,533
30 votes
Accepted

Printing 1,000,000 numbers in 1 sec. in Python

Nice solution, few suggestions: Printing the numbers one by one might be the issue. Generate the list of numbers first and then call print only once. The case <...
user avatar
  • 5,487
28 votes

Project Euler 001 in C++

I am not focussing on the algorithm, but more on C++ idioms. Very good statement of what you want out of this. Great! I suggest you familiarize yourself with the Core Guidelines being cataloged by ...
user avatar
  • 11.1k
27 votes
Accepted

Prime numbers that are also a prime number when reversed

Name Classes properly. What is a "Back Wards Prime"? I have no idea. I can guess what a Backward Prime is; perhaps you need to change the capitalization of the class! I'm told that name is ...
user avatar
  • 30.7k
26 votes
Accepted

"Longest Collatz sequence" in C slower than in Python 3

Assuming int is 32 bit long on your system, an integer overflow occurs on c with upper = 1000000. The multiplication in ...
user avatar
  • 376
25 votes
Accepted

What does the Bob say?

Avoid if chains with enumerations You are using an enumeration for all the possible responses of Bob. This is a great idea, and you can take it a step further. ...
user avatar
  • 9,195
25 votes
Accepted

Check if a string has all unique characters

People are very eager to present their own solutions, so let me attempt an actual review of your code: Your names could be clearer. Strings is not very descriptive,...
user avatar
  • 4,452
25 votes
Accepted

HackerRank Implement Queue using two stacks Solution

Algorithmic complexity for combined operations Containers are interesting elements in most programming languages: they have an internal state, namely their elements and therefore their size. This ...
user avatar
  • 18.8k
24 votes
Accepted

LOL'ing-Up Project Euler One

Same Guy, Same Language, same criticism? Let me start by (again) critzising your variable casing choice of SHOUTCASE It's so darn hard to tell what's a variable and what's not :( But your names got ...
user avatar
  • 25k
24 votes
Accepted

Google Foobar Challenge: Lucky Triples

1. Testing Let's take a look at your second problem: I've tested it on rather trivial cases, but have no way of knowing [...] if it returns the correct answer in more intricate examples A good ...
user avatar
  • 49.2k
23 votes
Accepted

Perm-Missing-Elem: 100% functional score, but only 60% performance

@chillworld is correct in his observation that the issue is related to the int overflow causing the problems in the performance tests. The more accurate solution to this is to use a ...
user avatar
  • 96.7k
23 votes
Accepted

Duplicate like a weapon, arrays like heaven

@tim suggested a way to do this with less memory by sorting the array. There's another way to do it with less memory without needing to sort. Let's say that \$k\$ is the duplicate value in the array. ...
user avatar
  • 16.1k
23 votes
Accepted

Find the next multiple of N that is a perfect square number

I'm afraid we need to use… math. Take any square number and look at its prime factorization. For example, \$ 100 = 2^2·5^2 \$; \$ 144 = 2^4·3^2 \$; \$ 729 = 3^6 \$. The thing that's common to all of ...
user avatar
  • 49.2k
23 votes

Python 3 function to rotate an image 90°

You can use NumPy module that's good with arrays and matrices. It has a built-in for exactly that purpose - ...
user avatar
  • 886
23 votes
Accepted

Project Euler #1 in C++

The code is basically fine. The if condition is perhaps a little complicated though. To interpret it you have to read "if not ((i is not a multiple of 3) and (i is not a multiple of 5))", ...
user avatar
22 votes
Accepted

Project Euler -problem 1

This is not a hack. if( ( i % 3 == 0 ) || ( i % 5 == 0 ) || ( ( i % 3 == 0 ) && ( i % 5 == 0 ) ) ) What you have here is essentially: ...
user avatar
22 votes

Project Euler #1: Multiples of 3 and 5

You don't need iteration at all for this problem. Consider; the sum of all numbers from 1 to n is equal to n*(n+1)/2. Also the sum of all numbers less than n that divides d equals d times the sum of ...
user avatar
  • 990
22 votes
Accepted

Find largest prime factor

The problem is: What is the largest prime factor of the number 600851475143? Your solution has a function named lpf, which computes the complete prime ...
user avatar
  • 18.3k
22 votes

Finding pairs of Amicable Numbers, up to 10000

...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible