AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

Nth root of any number algorithm - Python

Go down

Nth root of any number algorithm - Python

Post by Lucaci Andrei on Wed Feb 22, 2012 2:16 am

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'''
Created on Feb 22, 2012

@author: sin
'''
from __future__ import division

def root(nr, nth):
return pow(nr, 1 / nth)

def __check_int__(nr):
try:
int(nr)
return True
except:
return False

def ending(n):
endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
+ (['st', 'nd', 'rd'] + 7 * ['th']) * int(n)
n_nr = int(n)
ord = n + endings[n_nr - 1]
return ord

while True:
print """Nth root of any number.
"""
nr = raw_input("Number: ")
if nr == 'exit':exit()
nth = raw_input("Order of the root: ")
if nth == 'exit':exit()
if __check_int__(nr) == True and __check_int__(nth) == True:
print ending(nth), "root of", nr, "is", '~' + str(root(int(nr), int(nth))), 'with 11 digits.\n'
else: print "\nReceived string instead of integer.\nInvalid number or order.\n"

avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 26
Location : Cluj-Napoca

View user profile http://www.andreil.wgz.ro

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum