Created
April 10, 2018 02:49
-
-
Save greymd/efc0f69d76925e599c32844a767f653a to your computer and use it in GitHub Desktop.
dcコマンド素数メモ
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
## From: https://en.wikipedia.org/wiki/Dc_(computer_program) | |
## prime number | |
2p # A = 2; print A | |
3p # A = 3; print A | |
## スタックの先頭にある数とsqrt(処理中の数)を計算 | |
[ # func f-# (int n) int { | |
d # A = n(試す数) | |
l! # B = ! -- nと試し割りの割る数を用意 | |
d # | |
2 # | |
+ # | |
s! # ! = ! + 2 -- 割る数を2ずつふやす | |
% | |
0= | |
@ # if(A % B == 0) f-@(n) -- もし割り切れれば ^ = 0 する(3ステップ後に必ず抜ける) | |
l! | |
l^ | |
!<# # if(^ >= !) f-#(A) -- 試し割りをsqrt(A)まで繰り返す。素数ならここでループを抜ける。 | |
# return A | |
]s# # } | |
[ # func f-@ (int n) { -- break用のマクロ。さらにprintを防ぐために0をpush | |
s/0ds^ # / = n; ^ = 0; A = 0 | |
]s@ # } | |
[ # func f-& (int n) { -- 出力するだけ | |
p # print n | |
]s& # } | |
[ # func f-. (int n) { | |
d # A = 試す数 | |
d # B = A | |
v # | |
s^ # ^ = sqrt(B); | |
3 # | |
s! # ! = 3 | |
l#x # f-#(A) | |
0<& # if (0 < A) f-&(A) # 合成数ならf-@で0が積んであるのでprintされず。素数ならその数がそのまま積まれてる | |
2+ # A = A + 2 | |
l.x # f-.(A) | |
# } | |
] | |
ds.x # f-.(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment