Talk:Loops/Increment loop index within loop body: Difference between revisions

m
no edit summary
(Created page with "== increments the index such that the new index is now that prime== I must start with the index i 42.<br> I must increment i until it is prime, so 43. I must now increment i s...")
 
mNo edit summary
 
(4 intermediate revisions by 3 users not shown)
Line 4:
I must now increment i such that the new i is now 43?<br>
I interpreted the sentence ' increments the index such that the new index is now that prime ' to mean you wanted me to set i to 43+43 but this is not what the task specification says.--[[User:Nigel Galloway|Nigel Galloway]] ([[User talk:Nigel Galloway|talk]]) 14:41, 17 March 2019 (UTC)
 
<br>
-----
<br>
 
Yes, I see your point. &nbsp; How about: <br>════════════════════════════════════════════════════════════════════════════════
;Task:
Write a loop which:
::* &nbsp; starts the index (variable) at &nbsp; '''42'''
::* &nbsp; (at iteration time) &nbsp; increments the index by unity
::* &nbsp; if the index is prime:
::::* &nbsp; displays the count of primes found (so far) and the prime &nbsp; (to the terminal)
::::* &nbsp; increments the index such that the new index is now the (old) index plus that prime
::* &nbsp; terminates the loop when &nbsp; '''42''' &nbsp; primes are shown
════════════════════════════════════════════════════════════════════════════════
 
Would this be satisfactory? &nbsp; &nbsp; -- [[User:Gerard Schildberger|Gerard Schildberger]] ([[User talk:Gerard Schildberger|talk]]) 00:29, 19 March 2019 (UTC)
 
:Works for me.--[[User:Nigel Galloway|Nigel Galloway]] ([[User talk:Nigel Galloway|talk]]) 13:25, 21 March 2019 (UTC)
 
I had (originally) had wording in the task's requirements to make it clear(er) &nbsp; (ha!, not so much) &nbsp; that the index was to be (possibly ''also'') incremented &nbsp; (by the computer program) &nbsp; by the prime just found, but if done/interpreted another way and the index was incremented by the another "extra" &nbsp; ''+1'', &nbsp; (by &nbsp; '''do''' &nbsp; loop structure mechanism) &nbsp; the new index &nbsp; (a ''prime+prime'') &nbsp; is never a prime, and if the index was incremented &nbsp; (''+1'') &nbsp; by the &nbsp; '''do''' &nbsp; loop structure mechanism, &nbsp; no harm was done &nbsp; (that is, at the worst, an extra check for primality was performed for the new index &nbsp; ''prime+prime'' &nbsp; instead of &nbsp; ''prime+prime+1''. &nbsp; So the extra wording, as it turns out, wasn't necessary, but I wanted the incrementation to be clear. &nbsp; So this almost (did?) became a &nbsp; "Who's on first?" &nbsp; sort of a word mess. &nbsp; &nbsp; -- [[User:Gerard Schildberger|Gerard Schildberger]] ([[User talk:Gerard Schildberger|talk]]) 00:29, 19 March 2019 (UTC)
 
:Well, I suppose the advantage of this description is that it indicates why you should not do this!, +1 for those imperative languages that prevent it--[[User:Nigel Galloway|Nigel Galloway]] ([[User talk:Nigel Galloway|talk]]) 13:25, 21 March 2019 (UTC)
 
:: The task's requirements has been updated (to the above). &nbsp; &nbsp; -- [[User:Gerard Schildberger|Gerard Schildberger]] ([[User talk:Gerard Schildberger|talk]]) 07:54, 22 March 2019 (UTC)
 
To be clear, does this mean that the first Kotlin solution is an example of what isn't wanted? As I understand it, that solution checks if i is prime, and if it is, it replaces i with 2*i and then checks that. It looks like the Lua and Nanoquery solutions have the same issue, but I'm drifting away from languages that I know. --[[User:ReeceGoding|ReeceGoding]] ([[User talk:ReeceGoding|talk]]) 13:51, 23 June 2020 (UTC)
331

edits