Poor mans count, is it off-topic?

Recently we had the question "Project Euler Problem #12 - Highly divisible triangular number"

1. It was closed as the OP included how to use the function with the function's code.

The code as posted fails to run due to NameError: name 'num' is not defined on the last line. – Peter Taylor Apr 25 at 7:30

The function works, and so I don't think it should be closed as broken because of an NameError in the example usage, because num isn't declared. We allow this exception in other questions, even if half the program up for review is filled with them, not example input.

2. We shouldn't guess input to the function:

@Peilonrayz, which is what? The problem which this is attempting to solve is quite small and specific, so the code which solves it should run standalone without requiring the user to guess a value for an undocumented parameter. – Peter Taylor Apr 25 at 8:38

Another user in chat also held this view.

3. Because it takes a while on large inputs:

-1 from me because the Code does not solve the problem. I was not able to find the result. for small values of num the program finishes without printing a result and for larger numthe program hangs up without printing a result. – miracle173 Apr 26 at 16:18

Problems that exceed a time limit aren't off-topic on Code Review.

I don't think anyone's arguing for or against the first reason for closure. And the third has already been addressed. What should our views on the second be?

Should we close questions for naive implementations implementing limits, when they're not needed?

If you replace for n in range(1,num) with for n in itertools.count(1), then there would be no problem. Or if a user entered float('inf') if they knew about it and if it worked with range. They're a novice and doing the next best thing they know of.

• @SimonForsberg I don't understand what you mean by there being a difference. However, it's a bit pedantic to close something for including the example usage of euler_12(num), where num is undefined. – Peilonrayz May 10 '18 at 23:07
• If the example usage given doesn't run properly, then the code is broken (or appears to be broken if the example is wrong) and needs to be fixed before reviewing. In other words, treat the example usage as a test case. It should work before we review. – 1201ProgramAlarm May 11 '18 at 0:50
• @1201ProgramAlarm Agreed. That's one of the reasons I consider that question unfit for Code Review and voted to close. – Mast May 11 '18 at 7:34
• @Peilonrayz Closing for undefined num is excessive of course, because then it's obvious that you can just replace num with any value. Otherwise, I agree with 1201ProgramAlarm. – Simon Forsberg May 11 '18 at 7:52
• – Peilonrayz May 11 '18 at 9:04
• @1201ProgramAlarm If it was a unit test such as assert fn(5) == 10, but it failed, then that would be off-topic as it wouldn't work as intended. But that would be fn that has the problem, not the unit test. print(fn(a)) doesn't say anything about fn and so you wouldn't say the question is off topic because a is undefined. – Peilonrayz May 11 '18 at 10:33
• @Peilonrayz I'm not a 10k+ user, so I can't compare the situations. – Mast May 11 '18 at 10:37
• @Mast Here – Peilonrayz May 11 '18 at 11:06
• I assume that voting up this question means agreeing with poor mans count is off topic here. So an upvote from me. – miracle173 May 14 '18 at 20:33
• I already described in my comment that "Problems that exceed a time limit aren't off-topic on Code Review" is off Topic here. But nevertheless I am interested in what is the large Input one has to supply and how long is the while it takes for this Input? Did you try it out or is it only an assumption that such an Input exists? – miracle173 May 15 '18 at 8:25
• @miracle173 I read the code, and see if it should work. This is also how I perform code reviews, read the code, make changes. So I guess reading and understanding the code is only an assumption. – Peilonrayz May 15 '18 at 12:33
• This may be the way to perform code Reviews and it may also make sense to review code that does not work. The question is if the Code works, because this is a requirement for Code that will be reviewed here. And if you think it works then give a numthat I can plug in to see that it works. And tell me how Long I have to wait until I see a result. – miracle173 May 16 '18 at 21:35
• @miracle173 Run my code, and enter that number +1. – Peilonrayz May 16 '18 at 22:12

1. We shouldn't guess input to the function:

@Peilonrayz, which is what? The problem which this is attempting to solve is quite small and specific, so the code which solves it should run standalone without requiring the user to guess a value for an undocumented parameter. – Peter Taylor Apr 25 at 8:38

"We shouldn't guess input to the function" is not a great summary of my comment, although maybe my phrasing was too subtle and should have been more explicit. A more accurate summary would be "We shouldn't have to guess how to invoke the function".

If the code had executed and solved the stated problem without any modifications then clearly there would have been no problem.

If, instead of a non-working invocation, the question had included text along the lines of

The function takes one parameter, which is an upper bound on the solution. It should be used interactively by guessing a value of the parameter, and then increasing the guess until a solution is found.

then there would also have been no problem. The fact that the code was not standalone would then have been a problem with the code (and therefore an issue to raise in the review) rather than a problem with the question. In fact, revisiting the question, I propose that adding that text might be a constructive way forward.

• Are you saying that if a question doesn't say how the code should be invoked, it should be closed? – Peilonrayz May 14 '18 at 8:37
• @Peilonrayz, one of our close reasons is "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used." – Peter Taylor May 14 '18 at 9:05
• Yes it is. What does that have to do with my comment? – Peilonrayz May 14 '18 at 9:15
• Also your last comment is a different close reason to the one you used. Did you pick the wrong one, or is there a different reason you voted to close the question? – Peilonrayz May 14 '18 at 12:27
• With the example usage the code should be on-topic, as it looks like a user posted a part of their code. As for removing it, does that mean you think these should be off-topic too? 1 2 3 4 5 6 They're answered by you, and don't have any comments, what's different? – Peilonrayz May 14 '18 at 14:10
• @Peilonrayz: There is a difference between "how do I call this method?" and "what is the intended strategy of repeatedly (and possibly tactically) calling this method to achieve a certain result?". Peter's issue focuses on the latter case; which seems to be the case here. How to invoke a simple one-off method seems to no be part of what Peter is focusing on (Peter, correct me if I'm wrong). – Flater May 16 '18 at 12:17
• @Flater, I think I'm focussing on "How do I call this method?" at the level (since how? can be interpreted at various levels) of "What does the argument mean?" – Peter Taylor May 16 '18 at 13:40
1. Is not an issue as that can be solved with just entering your own value. It is related to number 2, however.

2. This is actually a good point that the code itself does not solve the problem, one needs to manually enter various values of num in order to solve the problem. I don't think this is close worthy though, as it should still be possible to solve the problem by using the function in the question.

3. Not only does it take a while in large inputs, there is actually a bug in the code that probably causes it to never finish. This, is close worthy.

Because I am so nice though, I have added a comment indicating the bug with the code and suggested a solution:

The problem asks for What is the value of the first triangle number to have over five hundred divisors? but your code says if len(list) == 500:. Have you tried with if len(list) > 500: ?

• I agree with this, and agree with wanting to close for using == rather than >. – Peilonrayz May 11 '18 at 9:41
• I don't think that is a bug that can cause i tho never finish. if len(list) == 501 would be correct, because if your reach 501 you have more than 500 divisors. But the eual sign is not the Problem because the length of the list is checked every time an item is added. – miracle173 May 17 '18 at 10:48
• @miracle173 The problem is that there probably is no triangular number that has exactly 500 divisors. So the equal sign is a problem. If it would be >= or > then it would work. – Simon Forsberg May 17 '18 at 10:57
• no, that is not true. The list is incremented one by one and even if the number has 600 Divisors the program prints the number if the 500th Divisor is found and does not seach for the remaining 100 Divisors of this number. – miracle173 May 17 '18 at 12:02
• @miracle173 is correct. That means the code works if you use euler_12(12376). I changed the code to run in a fraction of the time to check. – Peilonrayz May 17 '18 at 12:13
• @Peilonrayz - your gist code suffers from the same problem that the OP's code has, the break only exits the inner loop, so with euler_12(1000000) you will continue to execute even after finding the value at 12376 – rolfl May 17 '18 at 12:33
• @rolfl so? It'll still output the wanted number. And I only made minor changes, so people agree that it's roughly the same code, and the same algorithm. – Peilonrayz May 17 '18 at 12:34
• @Peilonrayz - I am not trying to make you defensive here.... just pointing out that the OP's code does not terminate when it encounters the first matching value. This is just another reason why the OP's post is off-topic, it prints all matching values in the input range, not just the first, so it does not work as intended. – rolfl May 17 '18 at 12:37
• @rolfl it outputs the first, why should it matter if it outputs more? If someone wants a knife a Swiss army knife would work, even though it's more than just a knife. – Peilonrayz May 17 '18 at 12:37
• @Peilonrayz - from the challenge: What is the value of the first triangle number to have over five hundred divisors? - not what is the value of all. – rolfl May 17 '18 at 12:38
• @rolfl Ok, and you get the first from the code. I don't see what the problem is. – Peilonrayz May 17 '18 at 12:40
• @Peilonrayz I tested it and it works: you supplied a value to call the function and it terminates in about 13 seconds. so it would be a perfect candidate for a Code Review. You should post it here. – miracle173 May 17 '18 at 17:26
• @miracle173 So you agree, if you ignore the time it takes to complete the code works? Great! – Peilonrayz May 17 '18 at 17:29
• @miracle173 Huh, it seems like I fooled myself. Indeed the list can have 500 elements at some point and thus print the number. – Simon Forsberg May 17 '18 at 20:52

I already stated that I was not able to find a value for num and it seems that the OP wasn't able, too.

• Code presented here must be working.

If it's doubtful that the code is working it's up to the OP to show that the code is working. To see if a python code is working usually I run it. And if it does not produce the expected result or no result at all, then it does not work for me.

• time-limit-exceeded

The description of the 'time-limit-exceeded' tag says:

Time-limit-exceeded issues are a special kind of performance problem, where the code handles small test cases very well, but takes an unreasonable amount of time to complete for larger inputs. You may use this tag instead of performance when an online judge rejects your solution to a programming-challenge due to time constraints, or when you need to interrupt your computation because it appears to take much too long to finish.

For programs rejected by an online judge i may be that it may happen that the program finished after some seconds and that may be to Long for the constes but isn't a Problem for a user who wants to see the result of this program. But if the program runs too long

• I don't think that it is our job to prove the correctness of a program, nor is it our job to find errors in a program.

In your post you cite my comment and say that "problems that exceed a time limit aren't off-topic on Code Review". But this makes no sense. In the description of the 'time-limit-exceeded' tag it is stated that such programs will run for small input values but not scale well and therefore do not finish for larger values in a reasonable time. So to make it on topic in the sense of the 'time-limit-exceeded' tag (which I don't like) it is at least necessary to write a function that reads a number and calculates the smallest integer where the the number of factors is larger or equal the input number. So the question is off topic here because it was not able to calculate any meaningful result in reasonable time for any input.

• The way Stack Exchange has structured its close system follows the notion innocent until proven guilty. That is someone proves you're breaking a close reason. You can also change the 500 to the PE number and it IIRC runs fine. Laziness on our part shouldn't mean that a question should be closed. – Peilonrayz May 15 '18 at 12:20
• @Peilonrayz Got a link with that? – Mast May 15 '18 at 14:21
• @Mast A link to explain how the close system works? – Peilonrayz May 15 '18 at 15:12
• @Peilonrayz About the notion of "innocent until proven guilty". I'm pretty sure that won't hold up for every close reason, since explaining "Unclear what you're asking" would get quite complicated if it has to be proven the question can't be properly understood. – Mast May 15 '18 at 15:42
• @Mast You don't go into every question thinking it's UWYA and try to prove to yourself, and others, it's not. It's the other way around. – Peilonrayz May 15 '18 at 15:45
• @Peilonrayz Agreed. But proof still doesn't (necessarily) enter the equation when voting to close as UWYA. – Mast May 15 '18 at 15:51
• @Mast I agree, the proof doesn't have to be too in-depth. "I'm confused what X is" would be valid proof on an UWYA. Then it comes down to other users agreeing that they're confused what X is. You don't have to comment, but others should look for a reason, proof, it's the close reason. – Peilonrayz May 15 '18 at 15:56
• @Peilonrayz actually I proved that it didn't work. I tried a lot of values and none of it produced a result. What is the problem if a bad question is closed. The user can post a sufficient good one. Or improve the current one, if possible. – miracle173 May 15 '18 at 21:32
• @miracle173 You didn't wait for larger inputs to finish. "and for larger numthe program hangs up without printing a result". How's that proof? – Peilonrayz May 16 '18 at 13:54
• @Peilonrayz How Long do you think should I wait? – miracle173 May 16 '18 at 21:42
• @Peilonrayz what is a 'PE number' and 'IIRC' that you mention in this comment? – miracle173 May 16 '18 at 21:45
• @miracle173 Until the end of execution, which is likely to take a while. By PE number, I mean the one provided by Project Euler in the example, 5. If I Recall Correctly using that number works fine with OP's code. My problem with your 'proof' is that you gave up half way through trying. – Peilonrayz May 16 '18 at 22:16
• @Peilonrayz "Until the end of execution" isn't a satisfying answer. what if the execution takes 1 hour or 200000000 years or there is an infinite Loop? I need a practical answer that is useful for such cases. – miracle173 May 17 '18 at 17:09
• @miracle173 The requirements on Code Review are just that it works. It doesn't matter how long it takes. My comment on Simons answer shows it does in under a minute. – Peilonrayz May 17 '18 at 17:14

The question is not off-topic

1. This shouldn't be an issue as a we can just enter a number. It also isn't an error with the code but the example-usage code.

Say we are asking for a review of the following function:

def fn(num):
return num ** 2 / num

The following example usage results in a NameError, but the error doesn't originate from the posted code, it originates in the example usage. Added with the fact we allow snippets of code to be posted num may be defined, but the definition may not be posted in the question. And so the code isn't broken due to this NameError.

fn(num)

The following example usage results in a ZeroDivisionError which originates from within the code, not the example usage. And so the code may not be working as intended, which means that we then have to check the requirements whether this is off-topic.

fn(0)

2. This is saying we shouldn't guess how to invoke the function. To know how to invoke the function we would need to know the input to the function. And so this is the same as saying we need example input for code to be on-topic on Code Review.

We have never required example input for code to be on-topic on Code Review.

3. The code takes a while on large inputs. Optimizing the code so that the factors code runs in a fraction of the time shows the code works, and it runs in under a minute so you can check too. miracle173 agreed that the code works, and so his 'proof' it never ends isn't true.

One 'requirement' (I've not seen enforced anywhere else) is for the code to work on small values. If you change the values in the code to be the same as the example on Project Euler, then the code will work too.

And so with only a little amount of effort, changing the values to the ones on Project Euler, we would be able to tell the code works. Or we can optimise the code, and see that it still works.

4. @Peilonrayz - your gist code suffers from the same problem that the OP's code has, the break only exits the inner loop, so with euler_12(1000000) you will continue to execute even after finding the value at 12376 – rolfl♦ link

The requirement of PE is to get the first triangle number with over 500 factors. The code gets this value and so the code works as intended.

• "This is saying we shouldn't guess how to invoke the function. To know how to invoke the function we would need to know the input to the function. And so this is the same as saying we need example input for code to be on-topic on Code Review." Not at all. We need a clear statement of what the code does. That is fundamentally what is lacking in the question. – Peter Taylor May 22 '18 at 14:08