Quick actions

cmd+k|ctrl+k

Navigation

Languages

小学奥数题

Snippet info

Language

Raku

Visibility

public

Author

seaker

Created

2022-08-15T10:56:39.169404Z

Updated

2022-08-16T02:13:16.386676Z

# 若一个四位数,它的任意两位数字之差均等于它的某个数字,且每个数字都能作为差被取到。例如2022。而1234则不符合。问这样的数有多少个?

my $cnt = 0;

for 1000..9999 -> \n {
    my @a = n.comb.sort;
    if @a.combinations(2).map({ .[1] - .[0] }).sort.unique.join eq @a.unique.join {
        put n;
        ++$cnt;
    }
}

put "found $cnt such numbers";

# one liner:
# (1000..9999)».comb».sort.map(-> @a { +(@a.combinations(2).map({.[1]-.[0]}).sort.unique.join eq @a.unique.join) }).sum
INFO