fork download
  1. def get_bits(number):
  2. bits = 0
  3. while 2 ** bits < number:
  4. bits += 1
  5. return bits
  6.  
  7.  
  8. ms_per_year = 366 * 24 * 60 * 60 * 1000
  9. ms_per_year_bits = get_bits(ms_per_year)
  10.  
  11. sn = int('9' * 17)
  12. sn_bits = get_bits(sn)
  13.  
  14. min_amount = int('9' * 5)
  15. min_amount_bits = get_bits(min_amount)
  16.  
  17. target_bytes = 15
  18. target_bits = target_bytes * 8
  19. print(f'Target bytes: {target_bytes} ({target_bits} bits)')
  20.  
  21. for years_bits in range(6, 12):
  22. years = 2 ** years_bits
  23.  
  24. total_ms = ms_per_year * years
  25. total_ms_bits = get_bits(total_ms)
  26.  
  27. bits_wo_amount = years_bits + ms_per_year_bits + sn_bits
  28.  
  29. amount_bits = target_bits - bits_wo_amount
  30. amount = 2 ** amount_bits
  31.  
  32. free_bits = amount_bits - min_amount_bits
  33.  
  34. # let's check, if there will be difference in storing
  35. # year and the rest of the date/time together or separately
  36. # spoiler: no difference
  37. total_bits_separately = years_bits + ms_per_year_bits + sn_bits + amount_bits
  38. total_bits_together = total_ms_bits + sn_bits + amount_bits
  39.  
  40. print(f'Years: {years}')
  41. if total_bits_separately == total_bits_together:
  42. print(f' Total bits: {total_bits_separately}')
  43. else:
  44. print(f' Total bits (years and date/time separately): {total_bits_separately}')
  45. print(f' Total bits (years and date/time together): {total_bits_together}')
  46. print(f' Max amount: £{amount / 100} ({amount_bits} bits, {free_bits} free bits used)')
  47.  
Success #stdin #stdout 0.03s 9828KB
stdin
Standard input is empty
stdout
Target bytes: 15 (120 bits)
Years: 64
  Total bits: 120
  Max amount: £41943.04 (22 bits, 5 free bits used)
Years: 128
  Total bits: 120
  Max amount: £20971.52 (21 bits, 4 free bits used)
Years: 256
  Total bits: 120
  Max amount: £10485.76 (20 bits, 3 free bits used)
Years: 512
  Total bits: 120
  Max amount: £5242.88 (19 bits, 2 free bits used)
Years: 1024
  Total bits: 120
  Max amount: £2621.44 (18 bits, 1 free bits used)
Years: 2048
  Total bits: 120
  Max amount: £1310.72 (17 bits, 0 free bits used)