Skip to content

Instantly share code, notes, and snippets.

@davefernig
Created October 20, 2019 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davefernig/6fefc55177b079186ae6ac5fce03e50c to your computer and use it in GitHub Desktop.
Save davefernig/6fefc55177b079186ae6ac5fce03e50c to your computer and use it in GitHub Desktop.
from __future__ import division
from random import randint
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
SAMPLES_PER_N_VALUE = 10000
N_VALUES = [2**n for n in (range(10))]
sns.set()
def __has_mod(A, mod=2):
for i in range(len(A)):
if (A[i] % mod) == 0:
return i + 1
return len(A)
data = []
for N in N_VALUES:
for mod in (2, 3, 5, 10):
current_score = 0
for _ in range(SAMPLES_PER_N_VALUE):
A = [randint(0, 1000000) for _ in range(N)]
current_score += __has_mod(A, mod)
data.append([
N,
'mod={}'.format(mod),
current_score/SAMPLES_PER_N_VALUE,
])
data=pd.DataFrame(
columns=[
"N",
"Modulus",
"Avg runtime (over {} iterations)".format(SAMPLES_PER_N_VALUE),
],
data=data,
)
ax = sns.lineplot(
x="N",
y="Avg runtime (over {} iterations)".format(SAMPLES_PER_N_VALUE),
hue="Modulus",
style="Modulus",
data=data,
)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment