Strange numbers: Difference between revisions

J
(Ada version)
(J)
Line 1,287:
425 427 429 461 463 464 468 469 470 472
474 475 479 492 494 496 497</pre>
 
=={{header|J}}==
 
<lang J>isStrange=: ([: */ 2 3 5 7 e.&:|~ 2 -/\ 10 #.inv ])"0
 
strangePair=: (1 p: ::0:"0 i.10),. 0 1}.isStrange (+ 10&*)"0/~ i.10</lang>
 
To test if a number is strange, we check if the absolute value of the differences of adjacent pairs of digits are 2, 3, 5 or 7.
 
To count the number of ten digit strange numbers which begin with 1, we first build a table which associates each digit with the digits which have a prime difference from it. Then we take a list of lists where there's only one list which has the single element 1, for each list we find the digits which have a prime difference from the last digit in that list and append those digits each to copies of that list. Since each iteration here adds a single digit, nine iterations give us lists representing 10 digit numbers. We count those lists and that gives us the number needed for the stretch goal.
 
Task examples:
 
<lang J> (#~ 100&<:) I. isStrange i.500
130 131 135 136 138 141 142 146 147 149 161 163 164 168 169 181 183 185 186 202 203 205 207 241 242 246 247 249 250 252 253 257 258 270 272 274 275 279 292 294 296 297 302 303 305 307 313 314 316 318 350 352 353 357 358 361 363 364 368 369 381 383 385 386 ...
#{{ ;<@{{x,"1 0 I. ({:x){y }}&strangePair"1 y}}^:9 ,:1
853423</lang>
 
=={{header|Java}}==
6,951

edits