Inventory sequence: Difference between revisions

(J draft)
(→‎{{header|J}}: stretch)
Line 30:
=={{header|J}}==
 
<syntaxhighlight lang=J> nextinv=: ((*@] * 1+{.@[), }.@[ , ]) +/@({. = }.)
 
10 10$}.nextinv^:100]0 NB. first 100 elements of inventory sequence
Line 45:
({:,_2+#)nextinv^:(1000>{:)^:_]0 NB. first value of at least 1000 and its index
1001 24255</syntaxhighlight>
 
The inventory sequence has a "hidden value" which is the number that we are searching for, and counting. So, here, we include it as the first element of the representation of an inventory subsequence. And <code>nextinv</code> calculates both the next "hidden value" as well as the corresponding subsequence which incorporates a count of how many times the current "hidden value" appeared.
 
For the task, we iterate inductively from the initial state (either a 100 times or until we find a value which is greater than 1000).
 
<syntaxhighlight lang=J> stretch=: }.nextinv^:(10000>{:)^:_]0
 
(,~ {&stretch) {.I.2000<stretch NB. first value greater than 2000 and its index
2009 43301
(,~ {&stretch) {.I.3000<stretch
3001 61708
(,~ {&stretch) {.I.4000<stretch
4003 81456
(,~ {&stretch) {.I.5000<stretch
5021 98704
(,~ {&stretch) {.I.6000<stretch
6009 121342
(,~ {&stretch) {.I.7000<stretch
7035 151756
(,~ {&stretch) {.I.8000<stretch
8036 168804
(,~ {&stretch) {.I.9000<stretch
9014 184428
(,~ {&stretch) {.I.10000<stretch
10007 201788
require'plot'
plot 1e4{.stretch</syntaxhighlight>
[[File:J-invseq10k.png]]
 
=={{header|jq}}==
6,962

edits