David Hoelzer
David Hoelzer

Reputation: 16379

How to generate output while iterating with Del?

I'm knocking off 30+ years of rust and poking at APL again. I was working through some Project Euler questions and ran into an interesting problem. I defined the following function to evaluate "primeness":

prime←{0=+/0=⍵|⍨1↓⍳⌊⍵÷2}

This works fine for all reasonable cases, though it's clearly not the most efficient approach. However, the problem arises when I embed it in this conditional:

{⍺←6⋄prime ⍵ : (⍺+1)∇(⍵+1),⎕←⍺,⍵ ⋄ ⍺∇(⍵+1)}12

This works fine for the non-prime case, but creates an issue for primes since I'm now creating something that is the wrong shape by trying to generate output while performing the recursion with the ,⎕←⍺,⍵ clause.

The intention is the output which prime has been found and the value of the prime. I'm starting with ⍺←6 and ⍵←12 so the 6th prime, 13, is correctly found first.

Is there a way to use quad (or something) to generate some output while recursing?

Upvotes: 1

Views: 61

Answers (2)

Adám
Adám

Reputation: 7706

As an alternative to chaining with per LdBeth's answer, you can invert your condition, allowing multiple statements in the "else":

{⍺←6 ⋄ ~prime ⍵ : ⍺∇(⍵+1) ⋄ ⎕←⍺,⍵ ⋄ (⍺+1)∇(⍵+1)}12

Attempt this online!

Upvotes: 1

LdBeth
LdBeth

Reputation: 421

I guess you want instead of ,. Also note that your recursive definition does not have a terminate condition so it will run forever until manual interrupt.

      {⍺←6 ⋄ ∨/prime⍤0⊢⍵:(⍺+1)∇(⍵+1)⊣⎕←⍺,⍵ ⋄ ⍺ ∇(⍵+1)}12
6 13
7 17
8 19
9 23
10 29
11 31
12 37
13 41
14 43
15 47
16 53
17 59
18 61
19 67
...

Upvotes: 1

Related Questions