Printer-friendly versionPrinter-friendly version

This section describes two methods for checking the primality of an integer n, one with order of growth Θ(√n), and a “probabilistic” algorithm with order of growth Θ(log n). The exercises at the end of this section suggest programming projects based on these algorithms.

Searching for divisors


(define (smallest-divisor n)
  (find-divisor n 2))
(define (find-divisor n test-divisor)
        (else (find-divisor n (+ test-divisor 1)))))
(define (divides? a b)
  (= (remainder b a) 0))


(define (prime? n)
  (= n (smallest-divisor n)))

The end test for find-divisor is based on the fact that if n is not prime it must have a divisor less than or equal to √n.[44] This means that the algorithm need only test divisors between 1 and √n. Consequently, the number of steps required to identify n as prime will have order of growth Θ(√n).

The Fermat test

The Θ(log n) primality test is based on a result from number theory known as Fermat’s Little Theorem.[45]

Fermat’s Little Theorem:

If n is a prime number and a is any positive integer less than n, then a raised to the nth power is congruent to a modulo n.




(define (expmod base exp m)
  (cond ((= exp 0) 1)
        ((even? exp)


The Fermat test is performed by choosing at random a number a between 1 and n - 1 inclusive and checking whether the remainder modulo n of the nth power of a is equal to a. The random number a is chosen using the procedure random, which we assume is included as a primitive in Scheme. Random returns a nonnegative integer less than its integer input. Hence, to obtain a random number between 1 and n - 1, we call random with an input of n - 1 and add 1 to the result:

(define (fermat-test n)
  (define (try-it a)
    (= (expmod a n n) a))


(define (fast-prime? n times)
  (cond ((= times 0) true)
        (else false)))

Probabilistic methods




[44] If d is a divisor of n, then so is n/d. But d and n/d cannot both be greater than n. [back]


Exercise 1.21


Exercise 1.22


(define (expmod base exp m)
  (cond ((= exp 0) 1)
        ((even? exp)
         (remainder (* (expmod base (/ exp 2) m)
                       (expmod base (/ exp 2) m))

Using this procedure, write a procedure search-for-primes that checks the primality of consecutive odd integers in a specified range. Use your procedure to find the three smallest primes larger than 1000; larger than 10,000; larger than 100,000; larger than 1,000,000. Note the time needed to test each prime. Since the testing algorithm has order of growth of Θ(√n), you should expect that testing for primes around 10,000 should take about √10 times as long as testing for primes around 1000. Do your timing data bear this out? How well do the data for 100,000 and 1,000,000 support the √n prediction? Is your result compatible with the notion that programs on your machine run in time proportional to the number of steps required for the computation?

Exercise 1.23


Exercise 1.24


Exercise 1.25


(define (expmod base exp m)
  (remainder (fast-expt base exp) m))


Exercise 1.26


(define (expmod base exp m)
  (cond ((= exp 0) 1)
        ((even? exp)
                       (expmod base (/ exp 2) m))


Exercise 1.27

Demonstrate that the Carmichael numbers listed in footnote 47 really do fool the Fermat test. That is, write a procedure that takes an integer n and tests whether an is congruent to a modulo n for every a < n, and try your procedure on the given Carmichael numbers.

Exercise 1.28




I’m out of league here. Too much brain power on dsialpy!

hermes replica bags

My firs comment

kontes seo agen bola berhadiah samsung S4 tiap bulan depo 20rb. after reading the article about , and understand me better understand what I should do next agen bola berhadiah samsung S4 tiap bulan depo 20rb. Your article gave me a lot of inspiration that can make me more excited. I want to see another piece of writing on this website. I say thanks to all my friends. Agen Bola Berhadiah Samsung S4 Tiap Bulan Depo 20rb.

hermes outlet

you love this? hermes outlet online at my estore wuzfDRnP

burberry touch

view burberry sunglasses ZAgGZkgH [URL= - burberry shop online[/URL - and get big save CoAvGolH

Knock off Oakleys

This is similar to the benefits being gained by running barefoot in semi-soft sand. And all on this makes one’s body healthier and stronger. People will base their decision to buy the shoe on consumer reviews and claims by the company. Pensate totalmente libero di andare al mio sito e l’indicazione-up per una Nessun costo di 35 Parte E-mail newsletter contenente un grande aiuto su tutte le caratteristiche del gioco!hogan outlet 脠 un singolo asse golf swing il modo migliore per colpire una palla? Alimentato dal successo del Moe Norman, che ha reso popolare il singolo asse approccio ed 猫 stata ampiamente riconosciuta per il potere, la coerenza e correttezza a impressione, infuria la discussione su. Ha pubblicato una idea identificata come “normali Golf” un sacco di decenni fa. Hogan era un tempo citato come dichiarando: “Moe 猫 l’unica persona che avrei attraversare la strada a guardare le palle colpite.” Hogan non era da solo.But soon after skiing or snowshoeing off the beginner route, the trail is long and difficult. Follow this path and the snow is usually consistent for skiing or snowshoeing. You may find snow waist deep in the higher elevations. What Shanty lacks in difficulty is more than made up for in its wonderful views and those soft-as-velvet sunsets over incomparable West Michigan hills. One of Michigan’s best cross country ski trails, Shanty Creek Resort Trails is located near Mancelona, MI. Trails’ printable online topo maps offer shaded and un-shaded reliefs, and aerial photos too! Use topographic map functionality to find elevation, print high resolution maps, save a PNG, or just learn the topography around Shanty Creek Resort Trails. Knock off Oakleys

1.2.6 Example: Testing for Primality | SICP in Clojure

that in fact how to do running a blog.

my weblog … mooduu

1.2.6 Example: Testing for Primality | SICP in Clojure

But God’s Word could and our Creator loves online nursing school him. There were also some online nursing school non-nude photos that feature the woman with the same fake tattoos Lively wore for her role in” The Town” and a photo that shows the actress’s face.
Motivation and Emotion, 26, 57-81. Thus, a victim counselor under the rule
would be protected by the right of conscious
law, medical professionals in New York City. She is the principle of equal treatment under the

Feel free to surf to my homepage - nursing schools in new york (Giuseppe)

Authentic Mychal Kendricks Jersey

Yet again, sneakers that will in shape upon this specific categoryhave lessgroundfeel, a lesser amount of freedom and in addition moreweightdue for you to better bottoms along with moredurableuppers. Job to find out additional with regards to solar panel technology data, be sure to check out your author’ ersus site exceptional no cost article in solar power [url= ]Michael Harris Jersey[/url] systems [url= ]Kendall Reyes Womens Jersey[/url] pertaining to household employ, along with the easiest way a knowledgeable screen workers. These are generally people linked to installing of your acquired products. Make inquiries your building contractors prioritize and look after these people. Even though mastering, you’ll be able to train the idea on the yoga exercise mat along with develop essentially the most cozy along with powerful strategy.A terrific covering restoration builder might be identified by his or [url= ]Manti Te’o Womens Jersey[/url] her reputations, certainly not by simply the amount that they harass anyone at your house along with organization.

Revision of 1.2.6 Example: Testing for Primality from 13 July 20

I am curious to find out what blog platform you happen to
be utilizing? I’m experiencing some small security problems with
my latest website and I’d like to find something more safeguarded.
Do you have any solutions?

Also visit my web page: Watch The Amazing Spider Man 2 Online

camiseta espana 2014…………………………… http://www.xn—……
camiseta espana 2014

camiseta italia 2012…….……………………
camiseta italia 2012

Post new comment

  • Lines and paragraphs break automatically.
  • Adds typographic refinements.

More information about formatting options