apl

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?


Solution

  • 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!