Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- This program computes and lists 30,000 integers of the Lucas sequence.
- For those who do not know, the Lucas sequence is basically the Fibonacci sequence but the sequence starts with 2 and 1 instead
- of 1 and 2. That sounds like a small change, but it completely changes the output.
- A university wrote a program in Maple (???) to calculate the first 200. I decided to step it up and calculate the first 10,000.
- Written in Python 3, there may be some computational inaccuracies but in Python, integers don't overflow as easily as in C.
- Be warned! You need a decent amount of RAM open to use it AND to open the output, and the time to compute can increase
- substantially when looping over 100,000 times. The output of 30,000 numbers is about 93 MB despite being an HTML file.
- For tech-savvy readers, I used an Intel Celeron CPU with a 4GB RAM board, and it took 22 minutes and ~41 seconds.
- Also, this program uses a slightly slower iterable method, which is preferable here. The recurence method used
- by many tutorials increases the computation time exponentially rather than logarithmically. The recurrence method slows down
- incredibly fast when calculating over the 35th number. It would take hours just to find 100 numbers. The iteration method is much
- longer written down, but insanely fast in practice.
- Even Longer Output https://drive.google.com/file/d/12XT4RSJhyGhiYXwa0ZFKNtSnIaoHwLYs/view?usp=drive_link
- """
- import os, os.path
- from sys import set_int_max_str_digits
- set_int_max_str_digits(65536) # LITERALLY too many digits at around 20574, where the digits exceed 4300.
- try: from timeit import default_timer # Imports software for a timer, remove this if you don't want it
- except ImportError:
- os.system("py -m pip install timeit") # Installs it if not installed already.
- from timeit import default_timer
- mode = "html" # can be "html", "csv", or "txt"
- # NOTE: CSV mode takes the least total storage space, but is hardly readable.
- # If you want to read the output, use "html" and open in a browser, or use "txt" and open in any text editor.
- if mode not in ["html", "csv", "txt"]: # Verifies the mode is supported:
- raise ValueError("Mode must be html, csv, or txt")
- output = 0 # Presets output...
- # Sets file path and name, writing to existing file.
- if os.path.exists(f"lucasnumbers.{mode}"): output = open(f"lucasnumbers.{mode}", "w")
- else: output = open(f"lucasnumbers.{mode}", "x") # Creates file if it does not exist.
- nums: str = ""
- def lucas(n): # "hand-made" and not from https://www.geeksforgeeks.org/lucas-numbers/
- # declaring base values
- # for positions 0 and 1
- a = 2
- b = 1
- if (n == 0) :
- return a
- # generating number
- for _ in range(2, n + 1) :
- c = a + b
- a = b
- b = c
- return b # end of the code "written by myself"
- start = default_timer() # Start timer, remove if not needed.
- for i in range(30000):
- print(i)
- if mode == "txt": nums += str(i + 1) + " : " + str(lucas(i)) + "\n"
- elif mode == "html": nums += f"<span style=\"font-weight: bold;\">{str(i + 1)}</span><span>: {str(lucas(i))}</span><br>"
- elif mode == "csv": nums += str(lucas(i)) + ","
- if mode == "csv": nums = nums[0:len(nums)-1]
- if mode != "csv": nums += f"Finished in: {default_timer() - start} seconds" # End timer and add to output. Remove if not needed.
- # Write the output in the correct format.
- if mode == "txt": output.write(nums)
- elif mode == "html": output.write(f"<!DOCTYPE HTML><html><head><title>10000 Lucas Numbers</title></head><body style=\"width:2095em;\">{nums}</body></html>")
- elif mode == "csv": output.write(nums)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement