modã®ä¸ç
ã¾ãä»ãã¡ããã¨ç解åºæ¥ã¦ãªãã³ã¼ããã
(define ( expmod base exp m ) ( cond (( = exp 0 ) 1 ) (( even? exp ) ( remainder ( square ( expmod bese ( / exp 2 ) m )) m )) ( else ( remainder ( * base ( expmod base ( - exp 1 ) m )) m )))) ( define ( square x ) ( * x x ))
ããã¯ç´ æ°æ§ããã¹ããããã§ã«ãã¼ãã¹ãã®ã³ã¼ãã§ãã
ã¯ã£ããè¨ã£ã¦ä½ããããã®ããã£ã±ãã»ã»ã»w
ãªã®ã§goshã使ã£ã¦ã¿ã¾ãã
gosh> (define ( expmod base exp m ) ( cond (( = exp 0 ) 1 ) (( even? exp ) ( remainder ( square ( expmod bese ( / exp 2 ) m )) m )) ( else ( remainder ( * base ( expmod base ( - exp 1 ) m )) m )))) ( define ( square x ) ( * x x ))expmod
expmodã«ã¤ãã¦å®ç¾©ãã¦ããé¨åã¯ã¡ããã¨åãã£ããã§ããã©ã»ã»ã»
ãããããããmodã¨ãexpã£ã¦ã»ã»ã»ããããä½ãåãããªãã®ã§ããã»ã»ã»
mod = (modularã®ç¥) 2åã®ä»»æã®æ´æ°a,bãèªç¶æ°nã§å²ã£ãä½ããçããã¨ã a, b 㯠n ãæ³ã¨ãã¦ååã§ããã¨ãã ãããã[tex:a \eq b ] (mod n) ã¨è¡¨ã ã¤ã¾ãnã§å²ã£ãæ´æ°a,bã®ä½ããåãã£ã¦äºã ããï¼ exp = (exponentiationã®ç¥)åªä¹ï¼ã¹ããããï¼ ããä¸ã¤ã®æ°å士ãç¹°ãè¿ãæãåãããã¨ããæä½ã®ã㨠ã¤ã¾ãææ°ã£ã¦äºããªï¼
ããå°ãåãããããæ¸ãã¦ä¸ãããã¼ã»ã»ã»
ã¨ããªãã¨ãæ¸ãã¦ãããã¡ã«ãªãããã£ãã®ãã§ã«ãã¼ãã¹ãã®
ã³ã¼ãwikipedeaã®ãé°ã§ä½ã¨ãªãåãã£ã¦æ¥ãã»ã»ã»ããï¼
ãã§ã«ãã¼ã®å°å®ç
(mod n)
a>n ãã¤a,nã両æ¹ç´ æ°ã§ããæ´æ°a,nããã£ãã¨ãã
(aã¯nã®åæ°ã§ã¯ãªãæ´æ°ã§ãã)
aãn-1ä¹ããæ°åãnã§å²ãã¨ä½ãã¯1ã«ãªã
ãã®å®çãè¸ã¾ããä¸ã§ç´ æ°æ§ã®ãã¹ãã«ã¤ãã¦èãã¦ã¿ããã¨æãã¾ã
ããç´ æ°ãªã (mod n)ãæç«ãã¦
ããåææ°ãªããã®éã«ãªãã¾ã
(éãã¾ãçã£ã¦ãã¨ã§ããâ)
åææ°ã§ããããã®æ¡ä»¶ã¯
1. a>n (aã¯nã®åæ°ã§ã¯ãªãæ´æ°ã§ãã)ãã¤a,nãå ±ã«ç´ æ°ã§ã¯ãªãæ´æ° 2. aã®n-1ä¹ããæ°åãnã§å²ã£ãæã®ä½ãã¯1ã«ãªããªã
ãããªæ¡ä»¶ã®a,nããã£ããããã¯åææ°ã¨ããäºã«ãªãã¾ã
ãã ã[æ´æ°]ã¨ããç®æã«æ³¨ç®ããã¦ããã¤ç´ æ°ã«1ã¯å«ã¾ãªãã®ã§
ãã©ã¡ã¼ã¿ã¼ã¯2以ä¸næªæºã¨ããæ¡ä»¶ã§ã¹ã¿ã¼ããã¾ã
ã§ã¯ããä¸åº¦ã³ã¼ããè¦ã¦ã¿ã¾ã
(define ( expmod base exp m ) ( cond (( = exp 0 ) 1 ) (( even? exp ) ( remainder ( square ( expmod bese ( / exp 2 ) m )) m )) ( else ( remainder ( * base ( expmod base ( - exp 1 ) m )) m )))) ( define ( square x ) ( * x x ))
ãã£ã±åããããã§åãããªãã»ã»ã»
å¾ããå°ããªã®ã«ãªãã»ã»ã»
ãã¨ãã®ãã§ã«ãã¼ãã¹ãã¯å®ç§ãªç´ æ°æ§ã®ãã¹ãã§ã¯ããã¾ãã
[確ççç´ æ°]ãåãããã¹ããªã®ã§ã
ã«ã¼ãã¤ã±ã«æ°(絶対çç´ æ°)ãªããã®ãåå¨ããããã§ã
ã«ã¼ãã¤ã±ã«æ°ã¯ãåææ°ãªã®ã«ã©ããªaã使ã£ã¦ã確ççã«
ç´ æ°ã ã¨å¤å®ããã¦ãã¾ãã¾ã
ã¡ãªã¿ã«10000 以ä¸ã®ã«ã¼ãã¤ã±ã«æ°ã¯
561, 1105, 1729, 2465, 2821, 6601, 8911 ã§ã
SICPã«ããã¨ãã§ã«ãã¼ãã¹ãã欺ãæ°ã¯ç¨ãªã®ã§
å
åãã§ã«ãã¼ãã¹ãã¯ä¿¡ç¨ãåºæ¥ãããã§ã
561ã§ç´ æ°ã ã¨é¨ããã¡ããã®ã«ãã§ã«ãã¼ãã¹ããä¿¡ç¨åºæ¥ãã¨è¨ããã¦ã
å°ãå¾®å¦ãªãã§ããã©ã»ã»ã»
ã¡ãªã¿ã«RSAã¢ã«ã´ãªãºã ã«ããç´ æ°å¤å®æ³ã¯å®å
¨ã®ããã§ãw