Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

Science project idea

Python explorer: prime sieve and Fibonacci golden ratio

One program finds primes with the Sieve of Eratosthenes and generates Fibonacci numbers whose consecutive ratio settles near 1.618. It links number patterns to loops and lists.

Make it yourself: 8 stepsGet a ready-made kit: Minor Projects

A drawing for computing and coding projects, not a photograph of this model.

What it shows

One program finds primes with the Sieve of Eratosthenes and generates Fibonacci numbers whose consecutive ratio settles near 1.618. It links number patterns to loops and lists.

The science

A prime has exactly two factors, 1 and itself. The sieve starts with all numbers marked as possible primes and crosses out the multiples of each prime found, leaving only the primes. In the Fibonacci sequence each number is the sum of the two before it, and the ratio of a number to the previous one gets closer and closer to the golden ratio, about 1.618.

Materials

Cost: free. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.

  • A computer, laptop or phone with a browser (free)
  • Python 3 with IDLE, or the free Thonny editor, or an online Python runner (all free)
  • A notebook and a pencil

Safety

Read before you start

This project uses safe, everyday materials. Still, read the rules below with a parent before you start.

Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.

All the safety rules

Step by step

8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.

  1. Create numbers.py and define def sieve(n): beginning with is_prime = [True] * (n + 1) and is_prime[0] = is_prime[1] = False.

  2. Add for i in range(2, int(n ** 0.5) + 1): and inside it if is_prime[i]: followed by for j in range(i * i, n + 1, i): is_prime[j] = False.

  3. Finish with return [i for i in range(n + 1) if is_prime[i]].

  4. Test with print(sieve(50)); the list should be 2, 3, 5, 7, 11, 13 and so on up to 47.

  5. Print how many primes there are below 100 and below 1000 using len(sieve(100)) and len(sieve(1000)), which should be 25 and 168.

  6. Write def fib(n): that builds a list starting [1, 1] and uses a loop with fibs.append(fibs[-1] + fibs[-2]) to reach n numbers.

  7. Print the ratios with for i in range(1, 15): print(fib(15)[i] / fib(15)[i - 1]) and watch them close in on 1.618.

  8. Write two lines of conclusion, and time sieve(100000) against checking each number by trial division, using import time and time.time() before and after each run.

What you should see

Expected result

The program prints the primes up to 50, finds 25 primes below 100 and 168 below 1000, and prints ratios that approach 1.618 as the Fibonacci numbers grow.

Viva questions

A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.

01What is a prime number?

A number with exactly two factors, 1 and itself.

02Why does the sieve stop at the square root of n?

Any composite number up to n has a factor no larger than its square root.

03How is each Fibonacci number made?

By adding the two before it.

04What is the golden ratio?

About 1.618, the limit of the ratio of consecutive Fibonacci numbers.

Free ebook: Viva Questions for Science Projects

Take it further

Make it your own

Plot the primes below 100 as a spiral using turtle (Ulam spiral) and look for diagonal lines.

Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.

Free ebooks for this project

Find out more about computing and coding

Free, well-known places to explore the subject further. Ask a grown-up before you sign up for anything or type your name anywhere.

WhatsApp us