# SICP Solutions

### Section - Procedures and the Processes They Generate

#### Exercise 1.23

Here I have written code in two ways:

• one is as asked in problem, by writing a function next-divisor.
• Without writing any extra function, just adding 2 in every iteration.

First approach:

Second approach:

Now I will put all the timings from all three approaches in one place:

Numbers Orignal approach with no skipping Skipping even numbers by calling next-divisor Skipping even numbers directly
1009 4 3 2
1013 4 3 2
1019 4 3 2
10007 12 9 7
10009 26 8 7
10037 13 7 5
100003 41 26 20
100019 40 27 19
100043 40 26 19
1000003 157 83 62
1000033 126 82 63
1000037 127 83 63
10000000000037 388727 279466 194440
10000000000051 388108 279058 192753
10000000000099 394797 304850 207065
100000000000031 1238697 8375991 6426951
100000000000067 1240527 8306311 6561041

As we can see clearly that ‘next-divisor’ function calls make significant extra cost and it increases as number increases. Without using this function and skipping the even numbers generates prime numbers in almost half of the orignial time as expected.