Finding the word version of a number [duplicate] - python
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Project Euler 17
So for this problem here of project euler:
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
I tried to figure this out myself with no help... but I think I am doing something wrong...
I have this code...
def convert(n):
if n < 10:
return placement(n,1)
elif n == 10:
return "ten"
elif n > 10 and n < 20:
return teen(n)
elif n >= 20 and n < 100:
if int(str(n)[1]) == 0:
return placement(str(n)[0],2)
else:
return placement(str(n)[0],2)+convert(int(str(n)[1]))
elif len(str(n)) == 3:
x = ""
h = placement(str(n)[0], 3)
if int(str(n)[1]) == 0 and int(str(n)[2]) == 0:
return h
else:
z = int(str(n)[1]+"0")+int(str(n)[2])
x = h + "and" + str(convert(z))
return x
elif len(str(n)) == 4:
x = ""
t = placement(str(n)[0], 4)
if int(str(n)[1]) == 0 and int(str(n)[2]) == 0 and int(str(n)[3]) == 0:
return t
def teen(n):
n = int(n)
if n == 11:
return "eleven"
if n == 12:
return "twelve"
if n == 13:
return "thirteen"
if n == 14:
return "fourteen"
if n == 15:
return "fifteen"
if n == 16:
return "sixteen"
if n == 17:
return "seventeen"
if n == 18:
return "eighteen"
if n == 19:
return "nineteen"
else:
return ""
def placement(n,p):
n = int(n)
if p == 1:
if n == 1:
return "one"
if n == 2:
return "two"
if n == 3:
return "three"
if n == 4:
return "four"
if n == 5:
return "five"
if n == 6:
return "six"
if n == 7:
return "seven"
if n == 8:
return "eight"
if n == 9:
return "nine"
else:
return ""
if p == 2:
if n == 1:
return "ten"
if n == 2:
return "twenty"
if n == 3:
return "thirty"
if n == 4:
return "fourty"
if n == 5:
return "fifty"
if n == 6:
return "sixty"
if n == 7:
return "seventy"
if n == 8:
return "eighty"
if n == 9:
return "ninety"
else:
return ""
if p == 3:
if n != 0:
return placement(n,1) + "hundred"
else:
return ""
if p == 4:
if n != 0:
return placement(n,1) + "thousand"
else:
return ""
z = 0
for x in range(1,1001):
z += len(convert(x))
print z
This spits out the answer 21224 but project euler says its wrong, so does anyone see whats wrong with what I am doing? and does anyone have a better way of doing this, if you do can you please explain it instead of just giving me some code?
sorry for this long bit... but if I print each line I get this...
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twentyone
twentytwo
twentythree
twentyfour
twentyfive
twentysix
twentyseven
twentyeight
twentynine
thirty
thirtyone
thirtytwo
thirtythree
thirtyfour
thirtyfive
thirtysix
thirtyseven
thirtyeight
thirtynine
fourty
fourtyone
fourtytwo
fourtythree
fourtyfour
fourtyfive
fourtysix
fourtyseven
fourtyeight
fourtynine
fifty
fiftyone
fiftytwo
fiftythree
fiftyfour
fiftyfive
fiftysix
fiftyseven
fiftyeight
fiftynine
sixty
sixtyone
sixtytwo
sixtythree
sixtyfour
sixtyfive
sixtysix
sixtyseven
sixtyeight
sixtynine
seventy
seventyone
seventytwo
seventythree
seventyfour
seventyfive
seventysix
seventyseven
seventyeight
seventynine
eighty
eightyone
eightytwo
eightythree
eightyfour
eightyfive
eightysix
eightyseven
eightyeight
eightynine
ninety
ninetyone
ninetytwo
ninetythree
ninetyfour
ninetyfive
ninetysix
ninetyseven
ninetyeight
ninetynine
onehundred
onehundredandone
onehundredandtwo
onehundredandthree
onehundredandfour
onehundredandfive
onehundredandsix
onehundredandseven
onehundredandeight
onehundredandnine
onehundredandten
onehundredandeleven
onehundredandtwelve
onehundredandthirteen
onehundredandfourteen
onehundredandfifteen
onehundredandsixteen
onehundredandseventeen
onehundredandeighteen
onehundredandnineteen
onehundredandtwenty
onehundredandtwentyone
onehundredandtwentytwo
onehundredandtwentythree
onehundredandtwentyfour
onehundredandtwentyfive
onehundredandtwentysix
onehundredandtwentyseven
onehundredandtwentyeight
onehundredandtwentynine
onehundredandthirty
onehundredandthirtyone
onehundredandthirtytwo
onehundredandthirtythree
onehundredandthirtyfour
onehundredandthirtyfive
onehundredandthirtysix
onehundredandthirtyseven
onehundredandthirtyeight
onehundredandthirtynine
onehundredandfourty
onehundredandfourtyone
onehundredandfourtytwo
onehundredandfourtythree
onehundredandfourtyfour
onehundredandfourtyfive
onehundredandfourtysix
onehundredandfourtyseven
onehundredandfourtyeight
onehundredandfourtynine
onehundredandfifty
onehundredandfiftyone
onehundredandfiftytwo
onehundredandfiftythree
onehundredandfiftyfour
onehundredandfiftyfive
onehundredandfiftysix
onehundredandfiftyseven
onehundredandfiftyeight
onehundredandfiftynine
onehundredandsixty
onehundredandsixtyone
onehundredandsixtytwo
onehundredandsixtythree
onehundredandsixtyfour
onehundredandsixtyfive
onehundredandsixtysix
onehundredandsixtyseven
onehundredandsixtyeight
onehundredandsixtynine
onehundredandseventy
onehundredandseventyone
onehundredandseventytwo
onehundredandseventythree
onehundredandseventyfour
onehundredandseventyfive
onehundredandseventysix
onehundredandseventyseven
onehundredandseventyeight
onehundredandseventynine
onehundredandeighty
onehundredandeightyone
onehundredandeightytwo
onehundredandeightythree
onehundredandeightyfour
onehundredandeightyfive
onehundredandeightysix
onehundredandeightyseven
onehundredandeightyeight
onehundredandeightynine
onehundredandninety
onehundredandninetyone
onehundredandninetytwo
onehundredandninetythree
onehundredandninetyfour
onehundredandninetyfive
onehundredandninetysix
onehundredandninetyseven
onehundredandninetyeight
onehundredandninetynine
twohundred
twohundredandone
twohundredandtwo
twohundredandthree
twohundredandfour
twohundredandfive
twohundredandsix
twohundredandseven
twohundredandeight
twohundredandnine
twohundredandten
twohundredandeleven
twohundredandtwelve
twohundredandthirteen
twohundredandfourteen
twohundredandfifteen
twohundredandsixteen
twohundredandseventeen
twohundredandeighteen
twohundredandnineteen
twohundredandtwenty
twohundredandtwentyone
twohundredandtwentytwo
twohundredandtwentythree
twohundredandtwentyfour
twohundredandtwentyfive
twohundredandtwentysix
twohundredandtwentyseven
twohundredandtwentyeight
twohundredandtwentynine
twohundredandthirty
twohundredandthirtyone
twohundredandthirtytwo
twohundredandthirtythree
twohundredandthirtyfour
twohundredandthirtyfive
twohundredandthirtysix
twohundredandthirtyseven
twohundredandthirtyeight
twohundredandthirtynine
twohundredandfourty
twohundredandfourtyone
twohundredandfourtytwo
twohundredandfourtythree
twohundredandfourtyfour
twohundredandfourtyfive
twohundredandfourtysix
twohundredandfourtyseven
twohundredandfourtyeight
twohundredandfourtynine
twohundredandfifty
twohundredandfiftyone
twohundredandfiftytwo
twohundredandfiftythree
twohundredandfiftyfour
twohundredandfiftyfive
twohundredandfiftysix
twohundredandfiftyseven
twohundredandfiftyeight
twohundredandfiftynine
twohundredandsixty
twohundredandsixtyone
twohundredandsixtytwo
twohundredandsixtythree
twohundredandsixtyfour
twohundredandsixtyfive
twohundredandsixtysix
twohundredandsixtyseven
twohundredandsixtyeight
twohundredandsixtynine
twohundredandseventy
twohundredandseventyone
twohundredandseventytwo
twohundredandseventythree
twohundredandseventyfour
twohundredandseventyfive
twohundredandseventysix
twohundredandseventyseven
twohundredandseventyeight
twohundredandseventynine
twohundredandeighty
twohundredandeightyone
twohundredandeightytwo
twohundredandeightythree
twohundredandeightyfour
twohundredandeightyfive
twohundredandeightysix
twohundredandeightyseven
twohundredandeightyeight
twohundredandeightynine
twohundredandninety
twohundredandninetyone
twohundredandninetytwo
twohundredandninetythree
twohundredandninetyfour
twohundredandninetyfive
twohundredandninetysix
twohundredandninetyseven
twohundredandninetyeight
twohundredandninetynine
threehundred
threehundredandone
threehundredandtwo
threehundredandthree
threehundredandfour
threehundredandfive
threehundredandsix
threehundredandseven
threehundredandeight
threehundredandnine
threehundredandten
threehundredandeleven
threehundredandtwelve
threehundredandthirteen
threehundredandfourteen
threehundredandfifteen
threehundredandsixteen
threehundredandseventeen
threehundredandeighteen
threehundredandnineteen
threehundredandtwenty
threehundredandtwentyone
threehundredandtwentytwo
threehundredandtwentythree
threehundredandtwentyfour
threehundredandtwentyfive
threehundredandtwentysix
threehundredandtwentyseven
threehundredandtwentyeight
threehundredandtwentynine
threehundredandthirty
threehundredandthirtyone
threehundredandthirtytwo
threehundredandthirtythree
threehundredandthirtyfour
threehundredandthirtyfive
threehundredandthirtysix
threehundredandthirtyseven
threehundredandthirtyeight
threehundredandthirtynine
threehundredandfourty
threehundredandfourtyone
threehundredandfourtytwo
threehundredandfourtythree
threehundredandfourtyfour
threehundredandfourtyfive
threehundredandfourtysix
threehundredandfourtyseven
threehundredandfourtyeight
threehundredandfourtynine
threehundredandfifty
threehundredandfiftyone
threehundredandfiftytwo
threehundredandfiftythree
threehundredandfiftyfour
threehundredandfiftyfive
threehundredandfiftysix
threehundredandfiftyseven
threehundredandfiftyeight
threehundredandfiftynine
threehundredandsixty
threehundredandsixtyone
threehundredandsixtytwo
threehundredandsixtythree
threehundredandsixtyfour
threehundredandsixtyfive
threehundredandsixtysix
threehundredandsixtyseven
threehundredandsixtyeight
threehundredandsixtynine
threehundredandseventy
threehundredandseventyone
threehundredandseventytwo
threehundredandseventythree
threehundredandseventyfour
threehundredandseventyfive
threehundredandseventysix
threehundredandseventyseven
threehundredandseventyeight
threehundredandseventynine
threehundredandeighty
threehundredandeightyone
threehundredandeightytwo
threehundredandeightythree
threehundredandeightyfour
threehundredandeightyfive
threehundredandeightysix
threehundredandeightyseven
threehundredandeightyeight
threehundredandeightynine
threehundredandninety
threehundredandninetyone
threehundredandninetytwo
threehundredandninetythree
threehundredandninetyfour
threehundredandninetyfive
threehundredandninetysix
threehundredandninetyseven
threehundredandninetyeight
threehundredandninetynine
fourhundred
fourhundredandone
fourhundredandtwo
fourhundredandthree
fourhundredandfour
fourhundredandfive
fourhundredandsix
fourhundredandseven
fourhundredandeight
fourhundredandnine
fourhundredandten
fourhundredandeleven
fourhundredandtwelve
fourhundredandthirteen
fourhundredandfourteen
fourhundredandfifteen
fourhundredandsixteen
fourhundredandseventeen
fourhundredandeighteen
fourhundredandnineteen
fourhundredandtwenty
fourhundredandtwentyone
fourhundredandtwentytwo
fourhundredandtwentythree
fourhundredandtwentyfour
fourhundredandtwentyfive
fourhundredandtwentysix
fourhundredandtwentyseven
fourhundredandtwentyeight
fourhundredandtwentynine
fourhundredandthirty
fourhundredandthirtyone
fourhundredandthirtytwo
fourhundredandthirtythree
fourhundredandthirtyfour
fourhundredandthirtyfive
fourhundredandthirtysix
fourhundredandthirtyseven
fourhundredandthirtyeight
fourhundredandthirtynine
fourhundredandfourty
fourhundredandfourtyone
fourhundredandfourtytwo
fourhundredandfourtythree
fourhundredandfourtyfour
fourhundredandfourtyfive
fourhundredandfourtysix
fourhundredandfourtyseven
fourhundredandfourtyeight
fourhundredandfourtynine
fourhundredandfifty
fourhundredandfiftyone
fourhundredandfiftytwo
fourhundredandfiftythree
fourhundredandfiftyfour
fourhundredandfiftyfive
fourhundredandfiftysix
fourhundredandfiftyseven
fourhundredandfiftyeight
fourhundredandfiftynine
fourhundredandsixty
fourhundredandsixtyone
fourhundredandsixtytwo
fourhundredandsixtythree
fourhundredandsixtyfour
fourhundredandsixtyfive
fourhundredandsixtysix
fourhundredandsixtyseven
fourhundredandsixtyeight
fourhundredandsixtynine
fourhundredandseventy
fourhundredandseventyone
fourhundredandseventytwo
fourhundredandseventythree
fourhundredandseventyfour
fourhundredandseventyfive
fourhundredandseventysix
fourhundredandseventyseven
fourhundredandseventyeight
fourhundredandseventynine
fourhundredandeighty
fourhundredandeightyone
fourhundredandeightytwo
fourhundredandeightythree
fourhundredandeightyfour
fourhundredandeightyfive
fourhundredandeightysix
fourhundredandeightyseven
fourhundredandeightyeight
fourhundredandeightynine
fourhundredandninety
fourhundredandninetyone
fourhundredandninetytwo
fourhundredandninetythree
fourhundredandninetyfour
fourhundredandninetyfive
fourhundredandninetysix
fourhundredandninetyseven
fourhundredandninetyeight
fourhundredandninetynine
fivehundred
fivehundredandone
fivehundredandtwo
fivehundredandthree
fivehundredandfour
fivehundredandfive
fivehundredandsix
fivehundredandseven
fivehundredandeight
fivehundredandnine
fivehundredandten
fivehundredandeleven
fivehundredandtwelve
fivehundredandthirteen
fivehundredandfourteen
fivehundredandfifteen
fivehundredandsixteen
fivehundredandseventeen
fivehundredandeighteen
fivehundredandnineteen
fivehundredandtwenty
fivehundredandtwentyone
fivehundredandtwentytwo
fivehundredandtwentythree
fivehundredandtwentyfour
fivehundredandtwentyfive
fivehundredandtwentysix
fivehundredandtwentyseven
fivehundredandtwentyeight
fivehundredandtwentynine
fivehundredandthirty
fivehundredandthirtyone
fivehundredandthirtytwo
fivehundredandthirtythree
fivehundredandthirtyfour
fivehundredandthirtyfive
fivehundredandthirtysix
fivehundredandthirtyseven
fivehundredandthirtyeight
fivehundredandthirtynine
fivehundredandfourty
fivehundredandfourtyone
fivehundredandfourtytwo
fivehundredandfourtythree
fivehundredandfourtyfour
fivehundredandfourtyfive
fivehundredandfourtysix
fivehundredandfourtyseven
fivehundredandfourtyeight
fivehundredandfourtynine
fivehundredandfifty
fivehundredandfiftyone
fivehundredandfiftytwo
fivehundredandfiftythree
fivehundredandfiftyfour
fivehundredandfiftyfive
fivehundredandfiftysix
fivehundredandfiftyseven
fivehundredandfiftyeight
fivehundredandfiftynine
fivehundredandsixty
fivehundredandsixtyone
fivehundredandsixtytwo
fivehundredandsixtythree
fivehundredandsixtyfour
fivehundredandsixtyfive
fivehundredandsixtysix
fivehundredandsixtyseven
fivehundredandsixtyeight
fivehundredandsixtynine
fivehundredandseventy
fivehundredandseventyone
fivehundredandseventytwo
fivehundredandseventythree
fivehundredandseventyfour
fivehundredandseventyfive
fivehundredandseventysix
fivehundredandseventyseven
fivehundredandseventyeight
fivehundredandseventynine
fivehundredandeighty
fivehundredandeightyone
fivehundredandeightytwo
fivehundredandeightythree
fivehundredandeightyfour
fivehundredandeightyfive
fivehundredandeightysix
fivehundredandeightyseven
fivehundredandeightyeight
fivehundredandeightynine
fivehundredandninety
fivehundredandninetyone
fivehundredandninetytwo
fivehundredandninetythree
fivehundredandninetyfour
fivehundredandninetyfive
fivehundredandninetysix
fivehundredandninetyseven
fivehundredandninetyeight
fivehundredandninetynine
sixhundred
.....
ninehundredandninetyseven
ninehundredandninetyeight
ninehundredandninetynine
onethousand
It looks right to me...
If I test it with their examples using for x in range(1, 6) comes out with 19
If I do len(convert(115)) it says 20 characters, but if I do len(convert(342)) it comes out with 24 and not 23... so what did I do wrong?
It looks like you need a dash between your *ty word (twenty, thirty, etc) and the one's digit (one, two, etc). For example, I believe from the description that you should be getting "twenty-one" instead of "twentyone" (which is not a word by the way).
You probably also want to change "fourty" to "forty".
As already noted, the word for 40 is forty, not fourty.
Note that the long series of if statements that check for about 30 separate cases can be replaced by array references. This will make the program half as long.
For instance, replace your teen and placement functions with the following:
def teen(n):
n = int(n)
if n//10 == 1:
return ["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"][n-10]
else:
return ""
def placement(n,p):
n = int(n)
if n < 1:
return ""
if p == 1:
return ["one","two","three","four","five","six","seven","eight","nine"][n-1]
if p == 2:
return ["ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"][n-1]
if p == 3:
return placement(n,1) + "hundred"
if p == 4:
return placement(n,1) + "thousand"
Also, replace the first 6 lines of convert with:
def convert(n):
if n < 10:
return placement(n,1)
if n < 20:
return teen(n)
if n < 100:
Related
How to solve Luhn algoritm
there is a lot of information about how to write Luhn algortim. I'm trying it too and I think that I'am very close to succes but I have some mistake in my code and dont know where. The test card is VALID card but my algorithm says otherwise. Don't you know why? Thx for help test = "5573497266530355" kazde_druhe = [] ostatni = [] for i in test: if int(i) % 2 == 0: double_digit = int(i) * 2 if double_digit > 9: p = double_digit - 9 kazde_druhe.append(p) else: kazde_druhe.append(double_digit) else: ostatni.append(int(i)) o = sum(ostatni) k = sum(kazde_druhe) total = o+k if total % 10 == 0: print(f"Your card is valid ") else: print(f"Your card is invalid ") Finally! Thank you all for your help. Now it is working :-) test = "5573497266530355" kazde_druhe = [] ostatni = [] for index, digit in enumerate(test): if index % 2 == 0: double_digit = int(digit) * 2 print(double_digit) if double_digit > 9: double_digit = double_digit - 9 kazde_druhe.append(double_digit) else: kazde_druhe.append(double_digit) else: ostatni.append(int(digit)) o = sum(ostatni) k = sum(kazde_druhe) total = o+k if total % 10 == 0: print(f"Your card is valid ") else: print(f"Your card is invalid ")
From this description 2. With the payload, start from the rightmost digit. Moving left, double the value of every second digit (including the rightmost digit). You have to check the digit position, not the number itself. Change to this: for i in range(len(test)): if i % 2 == 0:
This code works. :) I fixed you code as much as i could. test = "5573497266530355" #test = "3379513561108795" nums = [] for i in range(len(test)): if (i % 2) == 0: num = int(test[i]) * 2 if num > 9: num -= 9 nums.append(num) else: nums.append(int(test[i])) print(nums) print((sum(nums) % 10) == 0) I found where your code went wrong. On the line: for i in test: if int(i) % 2 == 0: It should be: for i in range(len(test)): if i % 2 == 0: You should not be using the element of the string you should be using the index of the element.
Fastest way to find prime in intervals
I tried searching StackOverflow and some other sources to find the fastest way to get a prime number within some interval but I didn't find any efficient way, so here is my code: def prime(lower,upper): prime_num = [] for num in range(lower, upper + 1): # all prime numbers are greater than 1 if num > 1: for i in range(2, num): if (num % i) == 0: break else: prime_num.append(num) return prime_num Can I make this more efficient? I tried finding my answer in Fastest way to find prime number but I didn't find prime numbers in intervals.
I wrote an equation based prime finder using MillerRabin, it's not as fast as a next_prime finder that sieves, but it can create large primes and you get the equation it used to do it. Here is an example. Following that is the code: In [5]: random_powers_of_2_prime_finder(1700) Out[5]: 'pow_mod_p2(27667926810353357467837030512965232809390030031226210665153053230366733641224969190749433786036367429621811172950201894317760707656743515868441833458231399831181835090133016121983538940390210139495308488162621251038899539040754356082290519897317296011451440743372490592978807226034368488897495284627000283052473128881567140583900869955672587100845212926471955871127908735971483320243645947895142869961737653915035227117609878654364103786076604155505752302208115738401922695154233285466309546195881192879100630465, 2**1700-1, 2**1700) = 39813813626508820802866840332930483915032503127272745949035409006826553224524022617054655998698265075307606470641844262425466307284799062400415723706121978318083341480570093257346685775812379517688088750320304825524129104843315625728552273405257012724890746036676690819264523213918417013254746343166475026521678315406258681897811019418959153156539529686266438553210337341886173951710073382062000738529177807356144889399957163774682298839265163964939160419147731528735814055956971057054406988006642001090729179713' or use to get the answer directly: In [6]: random_powers_of_2_prime_finder(1700, withstats=False) Out[6]: 4294700745548823167814331026002277003506280507463037204789057278997393231742311262730598677178338843033513290622514923311878829768955491790776416394211091580729947152858233850115018443160652214481910152534141980349815095067950295723412327595876094583434338271661005996561619688026571936782640346943257209115949079332605276629723961466102207851395372367417030036395877110498443231648303290010952093560918409759519145163112934517372716658602133001390012193450373443470282242835941058763834226551786349290424923951 The code: import random import math def primes_sieve2(limit): a = [True] * limit a[0] = a[1] = False for (i, isprime) in enumerate(a): if isprime: yield i for n in range(i*i, limit, i): a[n] = False def ltrailing(N): return len(str(bin(N))) - len(str(bin(N)).rstrip('0')) def pow_mod_p2(x, y, z): "4-5 times faster than pow for powers of 2" number = 1 while y: if y & 1: number = modular_powerxz(number * x, z) y >>= 1 x = modular_powerxz(x * x, z) return number def modular_powerxz(num, z, bitlength=1, offset=0): xpowers = 1<<(z.bit_length()-bitlength) if ((num+1) & (xpowers-1)) == 0: return ( num & ( xpowers -bitlength)) + 2 elif offset == -1: return ( num & ( xpowers -bitlength)) + 1 elif offset == 0: return ( num & ( xpowers -bitlength)) elif offset == 1: return ( num & ( xpowers -bitlength)) - 1 elif offset == 2: return ( num & ( xpowers -bitlength)) - 2 def MillerRabin(N, primetest, iterx, powx, withstats=False): primetest = pow(primetest, powx, N) if withstats == True: print("first: ",primetest) if primetest == 1 or primetest == N - 1: return True else: for x in range(0, iterx-1): primetest = pow(primetest, 2, N) if withstats == True: print("else: ", primetest) if primetest == N - 1: return True if primetest == 1: return False return False PRIMES=list(primes_sieve2(1000000)) def mr_isprime(N, withstats=False): if N == 2: return True if N % 2 == 0: return False if N < 2: return False if N in PRIMES: return True for xx in PRIMES: if N % xx == 0: return False iterx = ltrailing(N - 1) k = pow_mod_p2(N, (1<<N.bit_length())-1, 1<<N.bit_length()) - 1 t = N >> iterx tests = [k+1, k+2, k, k-2, k-1] for primetest in tests: if primetest >= N: primetest %= N if primetest >= 2: if MillerRabin(N, primetest, iterx, t, withstats) == False: return False return True def lars_last_modulus_powers_of_two(hm): return math.gcd(hm, 1<<hm.bit_length()) def random_powers_of_2_prime_finder(powersnumber, primeanswer=False, withstats=True): while True: randnum = random.randrange((1<<(powersnumber-1))-1, (1<<powersnumber)-1,2) while lars_last_modulus_powers_of_two(randnum) == 2 and mr_isprime(randnum//2) == False: randnum = random.randrange((1<<(powersnumber-1))-1, (1<<powersnumber)-1,2) answer = randnum//2 # This option makes the finding of a prime much longer, i would suggest not using it as # the whole point is a prime answer. if primeanswer == True: if mr_isprime(answer) == False: continue powers2find = pow_mod_p2(answer, (1<<powersnumber)-1, 1<<powersnumber) if mr_isprime(powers2find) == True: break else: continue if withstats == False: return powers2find elif withstats == True: return f"pow_mod_p2({answer}, 2**{powersnumber}-1, 2**{powersnumber}) = {powers2find}" return powers2find def nextprime(N): N+=2 while not mr_isprime(N): N+=2 return N def get_primes(lower, upper): lower = lower|1 upper = upper|1 vv = [] if mr_isprime(lower): vv.append(lower) else: vv=[nextprime(lower)] while vv[-1] < upper: vv.append(nextprime(vv[-1])) return vv Here is an example like yours: In [1538]: cc = get_primes(1009732533765201, 1009732533767201) In [1539]: print(cc) [1009732533765251, 1009732533765281, 1009732533765289, 1009732533765301, 1009732533765341, 1009732533765379, 1009732533765481, 1009732533765493, 1009732533765509, 1009732533765521, 1009732533765539, 1009732533765547, 1009732533765559, 1009732533765589, 1009732533765623, 1009732533765749, 1009732533765751, 1009732533765757, 1009732533765773, 1009732533765821, 1009732533765859, 1009732533765889, 1009732533765899, 1009732533765929, 1009732533765947, 1009732533766063, 1009732533766069, 1009732533766079, 1009732533766093, 1009732533766109, 1009732533766189, 1009732533766211, 1009732533766249, 1009732533766283, 1009732533766337, 1009732533766343, 1009732533766421, 1009732533766427, 1009732533766457, 1009732533766531, 1009732533766631, 1009732533766643, 1009732533766667, 1009732533766703, 1009732533766727, 1009732533766751, 1009732533766763, 1009732533766807, 1009732533766811, 1009732533766829, 1009732533766843, 1009732533766877, 1009732533766909, 1009732533766933, 1009732533766937, 1009732533766973, 1009732533767029, 1009732533767039, 1009732533767093, 1009732533767101, 1009732533767147, 1009732533767159, 1009732533767161, 1009732533767183, 1009732533767197, 1009732533767233]
Yes. In general, use: Sieve of Eratosthenes Miller-Rabin Primality Test Other options include trying a compiled language, like C++, but I think that isn't what you're looking for, since you've asked about Python.
Yes, you can make it more efficient. As has been mentioned, for very large numbers use Miller-Rabin. For smaller ranges use the Sieve of Eratosthenes. However, apart from that, your prime checker code is very inefficient. 2 is the only even prime number, which can save you doing half the work you do. You only need to check up to the square root of the number you are testing. In any pair of factors: f and n/f, one is guaranteed to be less then or equal to the square root of the number being tested. Once you find a factor then the number is composite. My Python is not good, so this is in pseudocode: isPrime(num) // Negatives, 0, 1 are not prime. if (num < 2) return false // Even numbers: 2 is the only even prime. if (num % 2 == 0) return (num == 2) // Odd numbers have only odd factors. limit <- 1 + sqrt(num) for (i <- 3 to limit step 2) if (num % i == 0) return false // No factors found so num is prime return true end isPrime
adding parenthesis in recursive function
I'm simply looking to add a parenthesis at the end of my recursive function.. I'm literally just missing the final parenthesis, but I can't figure out how to add it in! Any help is greatly appreciated! My code: def sum( n ): if n == 0: return '1' elif n == 1: return '(1+1)' elif n == 2: return '((1+1)+(1+1))' elif n == 3: return '(((1+1)+(1+1))+((1+1)+(1+1)))' else: return '((((1+1)+(1+1))+((1+1)+(1+1)))' + ')'sum_power2( n - 1 )
Just switch the order in the last row, so it would be def sum_power2( n ): if n == 0: return '1' elif n == 1: return '(1+1)' elif n == 2: return '((1+1)+(1+1))' elif n == 3: return '(((1+1)+(1+1))+((1+1)+(1+1)))' else: return '((((1+1)+(1+1))+((1+1)+(1+1)))' + sum_power2( n - 1 )+')'
Try this: def sum_power(n,tmp=''): tmp = '1' if not tmp else '(' + tmp + '+' + tmp + ')' if n == 0: return tmp else: n -= 1 return sum_power(n,tmp) print(sum_power(2))
Beginner in Python, can't get this for loop to stop
Bit_128 = 0 Bit_64 = 0 Bit_32 = 0 Bit_64 = 0 Bit_32 = 0 Bit_16 = 0 Bit_8 = 0 Bit_4 = 0 Bit_2 = 0 Bit_1 = 0 Number = int(input("Enter number to be converted: ")) for power in range(7,0,-1): if (2**power) <= Number: place_value = 2**power if place_value == 128: Bit_128 = 1 elif place_value == 64: Bit_64 = 1 elif place_value == 32: Bit_32 = 1 elif place_value == 16: Bit_16 = 1 elif place_value == 8: Bit_8 = 1 elif place_value == 4: Bit_4 = 1 elif place_value == 2: Bit_2 = 1 elif place_value ==1: Bit_1 = 1 Number = Number - (place_value) if Number == 0: print ("Binary form of"),Number,("is"),Bit_128,Bit_64,Bit_32,Bit_16,Bit_8,Bit_4,Bit_2,Bit_1 I want this loop to move to the next 'power' value when it fails the first if condition, but when I run it in an interpreter, the program keeps on running despite the first condition not being true. I only want it to move on the next conditions if the first condition turns out to be true. How can I do this? This is my first "big" program in python, and I'm having a hard time figuring this out. Any tips would be appreciated. Btw, the program is meant to convert any number from 1-255 to binary form.
If you want a loop to go to the next value, all you need to do is use the continue keyword: ... for power in range(7,0,-1): if (2**power) <= Number: place_value = 2**power continue ...
Just using "break" statement , to break the current loop when the condition is satisfied.
Trouble with Python Code. Objective is to code suffix during input
Objective: Write a function that takes an integer as its only parameter and returns the ordinal abbreviation for that integer as its only result. For example, if your function is passed the integer 1 then it should return the string "1st". If it is passed the integer 12 then it should return the string "12th". If it is passed 2003 then it should return the string "2003rd". Your function must not print anything on the screen. def convert (n): self.num = num n = int(self.num) if 4 <= n <= 20: suffix = 'th' elif n == 1 or (n % 10) == 1: suffix = 'st' elif n == 2 or (n % 10) == 2: suffix = 'nd' elif n == 3 or (n % 10) == 3: suffix = 'rd' elif n < 100: suffix = 'th' ord_num = str(n) + suffix return ord_num def main (): day = int(input("Enter the day:")) month = int(input("Enter the month:")) year = int(input("Enter the year:")) print("on the %n" %n, convert(day), "day of the %n" %month, convert(month), "month of the %n" %year, convert(year),", something amazing happened!") main() This is my code however it keeps saying I haven't defined n when I run it. But above I've already defined it so not sure what the problem is.
This is probably closer to what you want: def convert(n): n = int(n) suffix = '' if 4 <= n <= 20: suffix = 'th' elif n == 1 or (n % 10) == 1: suffix = 'st' elif n == 2 or (n % 10) == 2: suffix = 'nd' elif n == 3 or (n % 10) == 3: suffix = 'rd' elif n < 100: suffix = 'th' return str(n) + suffix def main (): day = int(input("Enter the day: ")) month = int(input("Enter the month: ")) year = int(input("Enter the year: ")) print("on the %s day of the %s month of the %s, something amazing happened!" % (convert(day), convert(month), convert(year))) main() There are few issues. You cannot use n in main() when you define it in convert(). Also %n is not a valid format character. You need to define suffix = '' when also want run the year through the conversion function as the year can be larger than 100. Also, you probably copied the code from within a class definition. I removed the self.