Talk:100 doors: Difference between revisions

(→‎An observation: make Incorrect Answers a section)
 
(5 intermediate revisions by 4 users not shown)
Line 2:
Some of the "solutions" given are incorrect. This problem is also a great example of the "Fence Post" condition often missed by programmers. Look over the solutions and ask yourself if the 100th door is shown to be open or closed.
: Yes -- because the doors in the problem are described with counting numbers and not zero-indexed, there are multiple incorrect solutions with the classic [https://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error fencepost error], listing 1 (not 0) but omitting 100. There wrong answers include Raku, SuperCollider, and Ursala. [[User:Jeremydouglass|Jeremydouglass]] ([[User talk:Jeremydouglass|talk]]) 00:35, 18 April 2020 (UTC)
::The Processing answer is wrong too. Note that indexing from 0 does not necessarily mean the solution is wrong: this only means that the second door is at index 1, etc. The C solution is correct, for instance. [[User:Hooch|Hooch]] ([[User talk:Hooch|talk]]) 07:45, 18 April 2020 (UTC)
----
 
== An observation ==
An observation: You're actually making 101 passes. 100 mutative, and one for reading the final state. I'm wondering if the wording of the task should be changed, as no way of reporting the final state within the first 100 passes immediately comes to mind. --[[User:Short Circuit|Short Circuit]] 00:14, 7 October 2007 (MDT)
Line 12 ⟶ 13:
::Actually, square numbers have an even number of factors, the odd count comes from the first iteration that opens all doors. --[[User:AlexLehm|AlexLehm]] 22:16, 19 October 2011 (UTC)
::: Integer factors of 5 -> [1, 5], Integer factors of 4 -> [1, 2, 4] (The integer square root gives the list an odd length - other factors are always paired with their matching quotients) [[User:Hout|Hout]] ([[User talk:Hout|talk]]) 07:59, 8 September 2016 (UTC)
It also seems that the number of doors left open is the same as the integer square root of the total number of doors. --[[User:Nig|Nig]] ([[User talk:Nig|talk]]) 14:30, 16 June 2023 (UTC)
----
 
Line 123 ⟶ 125:
</pre>
But in the examples it goes to 100, and not to 10, that means it's calculating 1000 doors, and not 100 doors? [[User:Rainb|Rainb]] ([[User talk:Rainb|talk]]) 07:03, 21 July 2014 (UTC)
 
== Invalid JavaScript solution ==
 
ES6:
 
// Array comprehension style
[ for (i of Array.apply(null, { length: 100 })) i ].forEach((_, i) => {
var door = i + 1
var sqrt = Math.sqrt(door);
if (sqrt === (sqrt | 0)) {
console.log("Door %d is open", door);
}
});
 
JavaScript does not have array comprehension, this results in a syntax error... [[User:MattDESTROYER|MattDESTROYER]] ([[User talk:MattDESTROYER|talk]]) 00:29, 3 December 2023 (UTC)
 
:Interesting. I tried it on TIO.RUN. they have 4 Javascript options, only SpiderMonkey accepts the syntax but gives incorrect output.
:Changing <code>console.log("Door %d is open", door);</code> to <code>console.log("Door " + door + " is open");</code> gives correct output.
:--[[User:Tigerofdarkness|Tigerofdarkness]] ([[User talk:Tigerofdarkness|talk]]) 14:01, 3 December 2023 (UTC)
3,022

edits