# 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:** 15

<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 18, 2022, 11:51pm UTC](https://racket.discourse.group/t/transforming-the-code-to-use-recursion/1236/15 "2022-08-18T23:51:23Z")

</div>

I think that an iterative approach is better suited to this particular problem. I wrote the following optimized code to generate all primes \< up-bound:

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

  ;; 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)
  ;; skip index 0 and even indexes since we know even numbers are not prime
  (for/list ([i (in-range 1 (bytes-length bstring) 2)]
             [val (in-bytes bstring 1 #f 2)]
             #:when (equal? val 1))
    i))

```

I wouldn't recommend using unsafe operations like this in most code, but this code is fairly performant:

```scheme
(time (length (primes (run-sieve 100000))))
cpu time: 1 real time: 1 gc time: 0
9592 

```

This is calculating the first 9592 primes in about 1ms, and that includes the O(n) length operation. Using a byte string for the representation saves a lot of memory allocation(and time).

---

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