I wrote a small program for Project Euler problem 41 (
https://projecteuler.net/problem=41 ).
--- project_euler_41.d
void main()
{
import math_common : primesLessThan;
import std.stdio : writeln;
import std.conv : parse;
import std.algorithm : permutations, canFind, filter, each, sort;
import std.utf : byCodeUnit;
import std.range : assumeSorted;
import std.array : array;
auto primes = primesLessThan(9_999_999UL).assumeSorted;
"1234567".byCodeUnit
.permutations
.filter!(a => primes.canFind(a.parse!uint))
.each!(a => a.writeln);
}
---
The problem is this prints a list of numbers. The task requires
only the largest, so the intuitive fix is to add `.array.sort[$ -
1].writeln` in place of the `each`, but this prints an array of
`1234567`s instead of the permutations. Does anyone know how to
sort the filter result without modifying the individual results?