Last active
November 1, 2015 04:25
-
-
Save dododas/0c76dcb471a5cd7011a9 to your computer and use it in GitHub Desktop.
Python generator for the sequence 1, 2,.., 9, 10, 20,.., 90, 100, 200,..,.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
''' | |
A python generator for the sequence 1, 2,.., 9, 10, 20,.., 90, 100, 200,.. up to n_max | |
Created by: Raibatak Das | |
''' | |
from math import floor, log10 | |
def logseq(n_max): | |
k = 1; | |
while (k <= n_max): | |
yield k | |
k += int( 10**floor(log10(k)) ) | |
## Examples: | |
# Generate tick mark locations for a logarithmic axis in a plot | |
n_max = 500 | |
list(logseq(n_max)) | |
# Compute cumulative sum of the sequence | |
from itertools import accumulate | |
n_max = 10000 | |
vals = list(logseq(n_max)) | |
cumulsum = list(accumulate(vals)) | |
# Plot cumulative sum | |
import matplotlib.pyplot as plt | |
plt.figure(facecolor="w") | |
plt.semilogy(cumulsum, "o:") | |
plt.xlabel("index") | |
plt.ylabel("cumulative sum") | |
plt.title("1 + 2 + ... + 9 + 10 + 20 + ... + 90 + 100 + ...") | |
plt.grid(True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment