# Transforming the code to use recursion

**URL:** <https://racket.discourse.group/t/transforming-the-code-to-use-recursion/1236>\
**Category:** Questions & Answers\
**Created:** [August 16, 2022, 7:00pm UTC](https://racket.discourse.group/t/transforming-the-code-to-use-recursion/1236 "2022-08-16T19:00:34Z")\
**Posts on this page:** 1\
**Showing post:** 18

<div class="post-metadata">

**Author:** ![jjsimpso](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jjsimpso/32/602_2.png) [@jjsimpso](https://racket.discourse.group/u/jjsimpso)\
**Post date:** [August 19, 2022, 1:30pm UTC](https://racket.discourse.group/t/transforming-the-code-to-use-recursion/1236/18 "2022-08-19T13:30:29Z")

</div>

Thanks for the fix in the `primes` function! I was looking at the total number of primes and never noticed.

I wouldn't remove the first `for` in `run-sieve` because that is the real sieve. `primes` was intended as a convenience function to just count the primes. But to be consistent with that I should remove 0 and 1 as primes in the byte string as well. Here's my updated version:

```scheme
;; up-bound is non-inclusive
(define (run-sieve up-bound)
  (define bstring (make-bytes up-bound 1))

  (unsafe-bytes-set! bstring 0 0)
  (unsafe-bytes-set! bstring 1 0)
  
  ;; mark even numbers after 2 so we can skip all even numbers in the for loop below
  (for ([i (in-range 4 up-bound 2)])
    (unsafe-bytes-set! bstring i 0))
  
  (for ([i (in-range 3 (sqrt up-bound) 2)])
    (when (unsafe-fx= (unsafe-bytes-ref bstring i)
                      1)
      (for ([multiple (in-range (* i i) up-bound (* i 2))])
        (unsafe-bytes-set! bstring multiple 0))))

  bstring)

;; get a list of prime numbers from the byte string generated by run-sieve
(define (primes bstring)
  (cons 2 
        ;; skip index 0 and even indexes since we know even numbers are not prime
        (for/list ([i (in-range 3 (bytes-length bstring) 2)]
                   [val (in-bytes bstring 3 #f 2)]
                   #:when (equal? val 1))
          i)))

```

---

_[View the full topic](https://racket.discourse.group/t/transforming-the-code-to-use-recursion/1236)._
