Looking for prime factors
Requires the Smallest-divisor process in the book to find the minimum factor of 199, 1999, 19999.
Scheme Code:
Main process:
Define the process of finding prime numbers
, greater than the test value, then it must be a prime number
can and Divisible, then not prime, the minimum factor is 2
If not, go back to the process Find-div, and then try to divide it with 2+1, loop
#lang Racket (define (square x) (*x x)); Definition filter (define (smallest-Div N) (Find-div N2)) (Define (Find-Div N TD) (Cond (>( Square TD) N) N) ((DIVD?TD N) TD) (Else(Find-div N (+ TD1)))) (define (DIVD?a B) (= (remainder b a)0) );p the Rimer process, which equals true if the condition is met, otherwise false, denoted by #t and #f, similar to bool (Define (Primer?N) (= N (smallest-div N)))
Test:
In:
(Primer? 3)
(Smallest-div 199)
(Smallest-div 1999)
(Smallest-div 19999)
Out:
#t
199
1999
7
C + + Code:
intSquareintx) { intsum = x*x; returnsum;}intFind_div (intNintTD) { if(Square (TD) >N) {returnN; } Elseif(! (n%TD)) { returnTD; } Else{find_div (n, TD+1); }}int_tmain (intARGC, _tchar*argv[]) { intA, b,real; CIN>> a >>b; Real=Find_div (A, b); cout<< Real <<Endl; return0;}
Construction and interpretation of SICP computer program 1.21 searching for factor Scheme, C + + implementation