LoginSignup
1
0

More than 3 years have passed since last update.

ワンライナーで最大公約数

Last updated at Posted at 2020-10-26

ちょっとズルかもしれませんが、因数分解コマンドfactorを使います。
あと、プロセス置換を使うのでbash推奨です。

comm -1 -2 \
    <(factor 123456789 | tr " " "\n" | sort) \
    <(factor 987654321 | tr " " "\n" | sort) \
| awk 'BEGIN{r=1}{r*=$1}END{print r}'

それぞれの数をfactorで素因数分解し、空白区切りを行区切りに変換してから行のsort。
commで共通行を抽出したのち、awkを使って共通行の掛け算を行っています。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0