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) (find2 ) (define (Find-Div N TD) ( cond(> ( Square TD) N) ((DIVDTD N) TD) c12/>(Else1)))))(define (DIVD, b) ( 0)); Primer procedure, if the condition is satisfied then equals true, otherwise false, denoted by #t and #f, similar to bool (define (PrimerN) (= 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; } Else if(! (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; return 0;}
Construction and interpretation of computer programs 1.21 searching for prime factors