bin_fibo.py

Created by schraf

Created on April 24, 2023

240 Bytes

Représentation binaire des 320 premiers termes de la suite de Fibonacci.

Remarques :

1 & n permet de récupérer le bit unité, ce sera “1” si le nombre est impair et “0” sinon. n » 1 est une autre façon de diviser un entier par 2 sans tenir compte des décimales.

Version avec la bibliothèque PIL :

from PIL import Image, ImageDraw

source = Image.new("RGB", (700, 700), color = "white")
draw = ImageDraw.Draw(source)

x, m = 0, 700
f = [0, 1]

for i in range(m):
  if i < 2: n = i
  else:
      f = f[1:] + [sum(f)]
      n = f[1]
  j = 0
  while n > 0:
   if 1 & n:
    draw.point((x, m - j), fill = 'black')
   j += 1
   n >>= 1
  x += 1

source.show()


from kandinsky import *

x = 0
f = [0, 1]

for i in range(320):
  if i < 2: n = i
  else:
   f = f[1:] + [sum(f)]
   n = f[1]
  j = 0
  while n > 0:
   if 1 & n: set_pixel(x, 222 - j, (0, 0, 0))
   j += 1
   n >>= 1
  x += 1

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.