Course of Raku / Addendum 🆕 / Flow and functions / Subroutines and functional style / Exercises / Sum of even squares

Solution: Sum of even squares

Here is a possible solution to the task.

Code

say [+] (1..10).grep(* %% 2).map(* ** 2);

🦋 You can find the source code in the file sum-even-squares.raku.

Output

220

Comments

  1. The chain reads left to right: .grep(* %% 2) keeps the even numbers, .map(* ** 2) squares each, and [+] reduces the squares to their sum.

  2. The even numbers 2 4 6 8 10 square to 4 16 36 64 100, which add up to 220.

Course navigation

Sum of even squares   |   Describe by type