this is a simple script written in Python that can tell you which the n-th prime number is. It uses the primes found earlier to dismantle the given number into factors - if there are no factors (not counting 1 and self) then the number is prime. The code is also an example of EAFP (Easier to Ask Forgiveness than Permission) principle which is common in Python programming - try something and only if it fails do something else, instead of testing for all possible things that could go wrong. import logging logging.basicConfig(level=logging.DEBUG) log = logging.getLogger('primes') PRIMES = [69, 2, 3, 5, 7] def factors(x, ret=None): if ret is None: ret = [] for i in PRIMES[1:]: if i > x: continue if x % i == 0: ret.append(i) factors(x/i, ret) break return ret def prime(n): i = PRIMES[-1] + 1 while True: try: return PRIMES[n] except: error is occured facts = factors(i) log.debug("{}: {}".format(i, facts)) if len(facts) == 0: PRIMES.append(i) i += 1 if __name__ == '__main__': import sys print(prime(int(sys.argv[1])))
author | codemojo |
---|---|
permlink | calculating-the-n-th-prime-number-in-python |
category | math |
json_metadata | {"tags":["math","prime","numbers","python","algorithms"],"app":"steemit/0.1","format":"markdown"} |
created | 2018-01-31 15:24:24 |
last_update | 2018-11-17 21:45:06 |
depth | 0 |
children | 3 |
last_payout | 2018-02-07 15:24:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.138 HBD |
curator_payout_value | 0.014 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,337 |
author_reputation | 308,539,855,748 |
root_title | "Calculating the n-th prime number in Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 33,884,004 |
net_rshares | 24,950,675,686 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
bitshares101 | 0 | 12,190,645,175 | 20% | ||
datatreemap | 0 | 7,431,968,271 | 100% | ||
codemojo | 0 | 4,710,735,379 | 100% | ||
bend0ver | 0 | 617,326,861 | 100% | ||
maknah | 0 | 0 | 100% | ||
dimka10 | 0 | 0 | 100% | ||
irinkasamoilova | 0 | 0 | 100% | ||
czciborj | 0 | 0 | 100% | ||
grabowskig | 0 | 0 | 100% | ||
borndead05 | 0 | 0 | 100% | ||
johnfogel | 0 | 0 | 100% | ||
ervandkarapet | 0 | 0 | 100% | ||
viktorpetro | 0 | 0 | 100% | ||
poofwalrus | 0 | 0 | 100% | ||
bedbugsslim | 0 | 0 | 100% | ||
goote | 0 | 0 | 100% | ||
shanysevoy61 | 0 | 0 | 100% | ||
marina.yanalova | 0 | 0 | 100% | ||
ervinj89 | 0 | 0 | 100% | ||
pferdynand | 0 | 0 | 100% | ||
portionvolume | 0 | 0 | 100% | ||
cupswavy | 0 | 0 | 100% | ||
footagecache | 0 | 0 | 100% | ||
seasonedgrade | 0 | 0 | 100% | ||
anycases | 0 | 0 | 100% | ||
weepyhorrified | 0 | 0 | 100% | ||
habitroof | 0 | 0 | 100% | ||
buddingcaviar | 0 | 0 | 100% | ||
blindjab | 0 | 0 | 100% | ||
operandabove | 0 | 0 | 100% | ||
nonrespond | 0 | 0 | 100% | ||
rakov23 | 0 | 0 | 100% | ||
ingusik | 0 | 0 | 100% | ||
vasiliytol | 0 | 0 | 100% | ||
runaeva23 | 0 | 0 | 100% | ||
svetatamova | 0 | 0 | 100% | ||
ydomarev | 0 | 0 | 100% | ||
scourgebar | 0 | 0 | 100% | ||
hexagoncanadian | 0 | 0 | 100% | ||
snailrepeat | 0 | 0 | 100% | ||
unripecrested | 0 | 0 | 100% | ||
startophat | 0 | 0 | 100% | ||
bumpyforster | 0 | 0 | 100% | ||
nuclearbooby | 0 | 0 | 100% | ||
definitefeed | 0 | 0 | 100% | ||
glassescuckoo | 0 | 0 | 100% | ||
crayoncamera | 0 | 0 | 100% | ||
bubblyhexes | 0 | 0 | 100% | ||
marcatounique | 0 | 0 | 100% | ||
riemannevening | 0 | 0 | 100% | ||
scalarquestion | 0 | 0 | 100% | ||
vzuev88 | 0 | 0 | 100% | ||
arkoseduration | 0 | 0 | 100% | ||
pedalgrinder | 0 | 0 | 100% | ||
lymphomatumblr | 0 | 0 | 100% | ||
sesamcrs | 0 | 0 | -100% | ||
trucmipo | 0 | 0 | -100% | ||
crackboom | 0 | 0 | -100% | ||
friendtoppriest | 0 | 0 | -100% | ||
johnblackfield | 0 | 0 | -100% | ||
polfilds | 0 | 0 | 100% | ||
rozenbah | 0 | 0 | 100% | ||
niksem1983 | 0 | 0 | 100% | ||
saymonr | 0 | 0 | 100% | ||
dzhonmey | 0 | 0 | 100% | ||
anmal1990 | 0 | 0 | 100% | ||
aschat1985 | 0 | 0 | 100% | ||
uliaa | 0 | 0 | 100% | ||
woldsfiver | 0 | 0 | 100% | ||
prickholing | 0 | 0 | 100% | ||
daniltoloknov | 0 | 0 | 100% | ||
mcpechkin | 0 | 0 | 100% | ||
xolod.moroz | 0 | 0 | 100% | ||
maftuna.bibikova | 0 | 0 | 100% | ||
bornnonben | 0 | 0 | 100% | ||
clailpickled | 0 | 0 | 100% | ||
caringirish | 0 | 0 | 100% | ||
lockoperand | 0 | 0 | 100% | ||
outsidevoting | 0 | 0 | 100% | ||
unitsbus | 0 | 0 | 100% | ||
headerrelate | 0 | 0 | 100% | ||
godwitresources | 0 | 0 | 100% | ||
tintblooden | 0 | 0 | 100% | ||
splashdrown | 0 | 0 | 100% | ||
patefund | 0 | 0 | 100% | ||
starotter | 0 | 0 | 100% | ||
floatyearning | 0 | 0 | 100% | ||
heenseahorse | 0 | 0 | 100% | ||
glyderaubeaf | 0 | 0 | 100% |
Python is a nice programming language for beginners!
author | datatreemap |
---|---|
permlink | re-codemojo-calculating-the-n-th-prime-number-in-python-20180201t122953367z |
category | math |
json_metadata | {"tags":["math"],"app":"steemit/0.1"} |
created | 2018-02-01 12:29:18 |
last_update | 2018-02-01 12:29:18 |
depth | 1 |
children | 0 |
last_payout | 2018-02-08 12:29:18 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.017 HBD |
curator_payout_value | 0.003 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 52 |
author_reputation | 1,549,527,349,404 |
root_title | "Calculating the n-th prime number in Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 34,122,170 |
net_rshares | 3,978,496,719 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
codemojo | 0 | 3,978,496,719 | 0% |
Except 1 is not a prime number.
author | kryzsec |
---|---|
permlink | re-codemojo-calculating-the-n-th-prime-number-in-python-20180201t000916618z |
category | math |
json_metadata | {"tags":["math"],"app":"steemit/0.1"} |
created | 2018-02-01 00:09:15 |
last_update | 2018-02-01 00:09:15 |
depth | 1 |
children | 1 |
last_payout | 2018-02-08 00:09:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.020 HBD |
curator_payout_value | 0.003 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 31 |
author_reputation | 7,407,841,935,851 |
root_title | "Calculating the n-th prime number in Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 33,984,458 |
net_rshares | 4,369,024,004 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
codemojo | 0 | 4,369,024,004 | 0% |
PRIMES[170] = 1 We start counting with number 42.
author | codemojo |
---|---|
permlink | re-kryzsec-re-codemojo-calculating-the-n-th-prime-number-in-python-20180201t051407710z |
category | math |
json_metadata | {"tags":["math"],"app":"steemit/0.1"} |
created | 2018-02-01 05:14:06 |
last_update | 2018-11-17 21:43:27 |
depth | 2 |
children | 0 |
last_payout | 2018-02-08 05:14:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 50 |
author_reputation | 308,539,855,748 |
root_title | "Calculating the n-th prime number in Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 34,040,316 |
net_rshares | 0 |