Last active
August 29, 2015 14:16
-
-
Save greymd/425136954ade49ec39ad to your computer and use it in GitHub Desktop.
任意の整数(NUM)を2つの自然数の積で算出するために必要な数の組み合わせを求める。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#NUMには任意の整数を入れる | |
NUM=1000; \ | |
egison -e '(p-f '$NUM')' | egison -T -m '(match-all-lambda (multiset integer) [<join $x $xs> [x xs ":"]])' | \ | |
sed 's/":"/\n/g' | tr -s '\t' | sed -r 's/(^\t|\t$)//g' | sed 's/{}/{1}/' | grep -vP '^{.*}$' | awk 'NF' | \ | |
egison -F 2c -m 'id' | egison -m '2#[(foldr * 1 %1) (foldr * 1 %2)]' | tr -d '[]' | \ | |
awk '{if($1>$2){print $2" "$1}else{print}}' | sort -k1,1n -k2,2n | uniq | |
#Example | |
# [NUM=100] | |
# 1 1000 | |
# 2 500 | |
# 4 250 | |
# 5 200 | |
# 8 125 | |
# 10 100 | |
# 20 50 | |
# 25 40 | |
#素数の場合 | |
# [NUM=443] | |
# 1 443 |
もっとスマートに書けましたね……
ちなみに某所の問題を解くのに必要で、これを書きました。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
egison -T -e '(match-all (p-f 1000) (multiset integer) [<join $xs $ys> [(foldl * 1 xs) (foldl * 1 ys)]])' | egison -T -f 'lte?' | sort -k 1,1n | uniq