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 ...
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:
<...
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 ...
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.
...
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}$$
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 <...
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 ...
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 ...
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 ...
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. ...
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,...
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 ...
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 ...
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 ...
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 ...
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. ...
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 ...
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 -
...
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))", ...
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:
...
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 ...
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 ...
22
votes
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
programming-challenge × 3284python × 975
algorithm × 659
java × 636
performance × 630
time-limit-exceeded × 494
c++ × 480
python-3.x × 428
beginner × 423
c# × 306
javascript × 285
strings × 212
primes × 178
c × 147
functional-programming × 114
array × 104
haskell × 97
python-2.x × 93
interview-questions × 93
dynamic-programming × 84
ruby × 76
recursion × 76
object-oriented × 75
palindrome × 71
combinatorics × 69