Advertisement
AngryLad

30000 Lucas Numbers

Sep 11th, 2023 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | Source Code | 0 0
  1. """
  2. This program computes and lists 30,000 integers of the Lucas sequence.
  3.  
  4. For those who do not know, the Lucas sequence is basically the Fibonacci sequence but the sequence starts with 2 and 1 instead
  5. of 1 and 2. That sounds like a small change, but it completely changes the output.
  6.  
  7. A university wrote a program in Maple (???) to calculate the first 200. I decided to step it up and calculate the first 10,000.
  8. Written in Python 3, there may be some computational inaccuracies but in Python, integers don't overflow as easily as in C.
  9.  
  10. 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
  11. substantially when looping over 100,000 times. The output of 30,000 numbers is about 93 MB despite being an HTML file.
  12. For tech-savvy readers, I used an Intel Celeron CPU with a 4GB RAM board, and it took 22 minutes and ~41 seconds.
  13.  
  14. Also, this program uses a slightly slower iterable method, which is preferable here. The recurence method used
  15. by many tutorials increases the computation time exponentially rather than logarithmically. The recurrence method slows down
  16. incredibly fast when calculating over the 35th number. It would take hours just to find 100 numbers. The iteration method is much
  17. longer written down, but insanely fast in practice.
  18.  
  19. Even Longer Output https://drive.google.com/file/d/12XT4RSJhyGhiYXwa0ZFKNtSnIaoHwLYs/view?usp=drive_link
  20. """
  21.  
  22.  
  23. import os, os.path
  24. from sys import set_int_max_str_digits
  25.  
  26. set_int_max_str_digits(65536) # LITERALLY too many digits at around 20574, where the digits exceed 4300.
  27.  
  28. try: from timeit import default_timer # Imports software for a timer, remove this if you don't want it
  29. except ImportError:
  30.     os.system("py -m pip install timeit") # Installs it if not installed already.
  31.     from timeit import default_timer
  32.  
  33. mode = "html" # can be "html", "csv", or "txt"
  34. # NOTE: CSV mode takes the least total storage space, but is hardly readable.
  35. # If you want to read the output, use "html" and open in a browser, or use "txt" and open in any text editor.
  36.  
  37. if mode not in ["html", "csv", "txt"]: # Verifies the mode is supported:
  38.     raise ValueError("Mode must be html, csv, or txt")
  39.  
  40. output = 0 # Presets output...
  41. # Sets file path and name, writing to existing file.
  42. if os.path.exists(f"lucasnumbers.{mode}"): output = open(f"lucasnumbers.{mode}", "w")
  43. else: output = open(f"lucasnumbers.{mode}", "x") # Creates file if it does not exist.
  44.  
  45. nums: str = ""
  46.  
  47. def lucas(n): # "hand-made" and not from https://www.geeksforgeeks.org/lucas-numbers/
  48.     # declaring base values
  49.     # for positions 0 and 1
  50.     a = 2
  51.     b = 1      
  52.     if (n == 0) :
  53.         return a
  54.     # generating number
  55.     for _ in range(2, n + 1) :
  56.         c = a + b
  57.         a = b
  58.         b = c
  59.     return b # end of the code "written by myself"
  60.  
  61. start = default_timer() # Start timer, remove if not needed.
  62.  
  63. for i in range(30000):
  64.     print(i)
  65.     if mode == "txt": nums += str(i + 1) + " : " + str(lucas(i)) + "\n"
  66.     elif mode == "html": nums += f"<span style=\"font-weight: bold;\">{str(i + 1)}</span><span>: {str(lucas(i))}</span><br>"
  67.     elif mode == "csv": nums += str(lucas(i)) + ","
  68.    
  69. if mode == "csv": nums = nums[0:len(nums)-1]
  70.  
  71. if mode != "csv": nums += f"Finished in: {default_timer() - start} seconds" # End timer and add to output. Remove if not needed.
  72. # Write the output in the correct format.
  73. if mode == "txt": output.write(nums)
  74. elif mode == "html": output.write(f"<!DOCTYPE HTML><html><head><title>10000 Lucas Numbers</title></head><body style=\"width:2095em;\">{nums}</body></html>")
  75. elif mode == "csv": output.write(nums)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement