Solving Project Euler #2 in J

wenderen1 pts0 comments

Rohan Prinja

Solving Project Euler #2 in J

Jun 20 2026

Table of contents

Problem statement

A direct solution

The while pattern

Doesn't J have a while. keyword?

Beating the odds

Keeping less state

Closing thoughts

References

Appendix

In this post I discuss a fun problem I recently solved using the J programming language. The problem is number 2 in Project Euler.

First, a quick disclaimer:

From the Project Euler FAQ: "[...] the rule about sharing solutions outside of Project Euler does not apply to the first one-hundred problems , as long as any discussion clearly aims to instruct methods, not just provide answers, and does not directly threaten to undermine the enjoyment of solving later problems. Problems 1 to 100 provide a wealth of helpful introductory teaching material and if you are able to respect our requirements, then we give permission for those problems and their solutions to be discussed elsewhere."

Problem statement🔗

Paraphrasing, the problem asks us to

Find the sum of all even Fibonacci numbers that are under 4,000,000.

The solution to this is straightforward in a programming language that supports the imperative paradigm. For example, in Python:

def solve() -> int:<br>prev, curr, result = 1, 2, 0<br>while curr 4e6:<br>if curr % 2 == 0:<br>result += curr<br>prev, curr = curr, prev + curr<br>return result

print(solve())<br>The result is 4613732. How would we solve this problem in J?

A direct solution🔗

Here's a direct solution. I'm still learning the language, so it's probably not very polished.

step =. monad define<br>delta =. (curr , 0) {~ 2 | curr =. {: y<br>(delta + {. y) , curr , curr + 1 { y

{. (step^:(4e6 > {:))^:_ (0 1 2)<br>This is a straightforward translation of the problem statement into code. Let's dissect it, starting with step.

step takes as input a 3-element array representing an accumulated value and two consecutive Fibonnaci numbers . It returns a new accumulated value and slides the two Fibonacci numbers forward . The new accumulated value is the same as the old one if the old second Fibonacci number was odd. Otherwise, it's increased by the old second Fibonacci number.

Let's run this at the J REPL:

step 0 1 2<br>2 2 3<br>step 2 2 3<br>2 3 5<br>step 2 3 5<br>2 5 8<br>step 2 5 8<br>10 8 13<br>The indented lines are our queries, and the non-indented lines are the result. We start with an accumulated value of 0, and increase it only if we see an even Fibonacci number in the third position of the array.

The 2-line function translates this behavior directly. Starting with the first line:

y represents the right-hand argument to step.

{: takes the last element of an array.

curr =. {: y assigns that value to curr.

2 | curr computes the remainder of curr when divided by 2.

(curr , 0) is a 2-element array consisting of curr and 0.

(2 | curr) { (curr , 0) returns the (2 | curr)-th element of (curr , 0).

~ is an adverb (function modifier) that flips its verb (function)'s operands (arguments). If x and y are two nouns (variables), then x f~ y is the same as y f x.

And so (curr , 0) {~ 2 | curr is the same as (2 | curr) { (curr , 0). I wrote it with the ~ because it reads more naturally to me.

The above result is assigned to delta. This is what we'll add to the accumulated value.

Onto the next line.

The result is composed of 3 elements joined by the , verb. They are: delta + {. y, and curr, and curr + 1 { y. Let's look at each one of them.

{. y is the first element of the 3-element array y. This is the accumulated value, to which we add delta, and that's the 1st element of the result.

curr is the 2nd element of the result.

1 { y is the 2nd element of the 3-element array y.

curr + 1 { y is curr summed with that 2nd element. This becomes the 3rd element of the result.

That concludes our analysis of step. Nothing too fancy there. Onto the next line:

{. (step^:(4e6 > {:))^:_ (0 1 2)<br>This line is an instance of a common J pattern (u^:v)^:_ y, where:

y is a noun (variable).

u is a monadic verb (single-argument function),

v is a monadic boolean verb (single-argument function that returns either 0 or 1), and

Here:

y is (initially) the 3-element array 0 1 2 discussed earlier.

u is the step function discussed earlier.

v is (4e6 > {:). It's a boolean verb that checks if the last element of an array (obtained by {:) is less than 4 million.

How does this pattern work?

The while pattern🔗

Let's discuss the pattern (u^:v)^:_ y. What's up with the two ^:s and the underscore _?

^: is the power adverb. When coupled with a numeric noun it applies a function as many times as the noun specifies.

*: 3 NB. *: is the "square" verb.<br>*: *: 3 NB. Square 3, and then square the result.<br>81<br>(*:^:2) 3 NB. Same as the previous line. Square 3, and then square the result.<br>81<br>(*:^:3) 3 NB. Apply the "square" verb thrice to 3.<br>6561<br>(*:^:4) 3 NB. Apply the "square" verb four times to 3.<br>43046721<br>Side note: in J, *:^:3 3 is not the same as (*:^:3) 3, which is why we added the parentheses. The former supplies an array 3 3...

curr element result step array verb

Related Articles