I know that this has been asked already, but I'm having trouble with how I should implement this so I'm asking here.
My python code so far is:
def calculate():
p = 10000 # dollars
n = 12 # months
r = 8 # interest %
t = float(raw_input("Type the number of year that the money will be compounded for:"))
b = p * r
a = b ** t
print(a)
calculate()
Math formula
Like John Coleman mentioned in the comments you didn't implement the formula at all.
In your code you just multiplied p with r and b to the power of t.
The right formula looks like this: p*(1+(r/n))**(n*t).
I would recommend you to read an article related to python basic operators you can find one on Python Course.
def calculate():
p = 10000
n = 12
r = .08
t = float(input("Type the number of year that the money will be compounded for:"))
formula = p*(1+(r/n))**(n*t)
return formula
print (calculate())
So you need to return a value since you are writing a function. I would say input all your values into the function directly like so:
def calculate(P, r, n , t):
exponent = n*t
parends = 1 + (r/n)
val = parends ** exponent
ans = P * val
return ans
print(calculate(10000, .08, 12, 1))
I would check out other resources to learn how to use functions. Codeacademy is a good one.
Here is the function just not broken into pieces:
def shorter(P, r, n, t):
return P*(1+(r/n))**(n*t)
print(shorter(10000, .08, 12, 1))
Related
num = 5
for i in range(1,num):
for j in range(1,num):
print(i * j, end="\t")
print()
I have this multiplication table and i need to sum all of the numbers given without using loops.
can anyone advise?
Mathematics makes any task easier.
Formula is (n(n+1)/2)^2. Just put n = num-1. num-1 because your for loop generates table upto number 4.
Code:
>>> num = 5
>>> sum = ((num*(num-1))/2)**2
>>> print(sum)
100.0
I think you want to sum all resulting multiplication then add few lines in your code.
num = 5
multi_list = []
for i in range(1,num):
for j in range(1,num):
multi_list.append(i*j)
print(sum(multi_list))
#100
I am trying to define a function for Fibonacci series but the code is not working. I can't resolve the issues and need help to fix the problem. Whenever I am trying to call this function, last value of the series comes always greater than n, which I don't want
def fib(n):
Series = [0,1]
if n>1:
while Series[-1]<=n:
c=Series[-2]+Series[-1]
Series.append(c)
if Series[-1]>n:
break
return Series
Your code is really good, just the indentation of the return is wrong. Just align it properly.
def fib(n):
Series = [0,1]
if n>1:
while Series[-1]<=n:
c=Series[-2]+Series[-1]
Series.append(c)
return Series
do you need something like this:
def fibo(n):
l = [0,1]
for i in range(2,n+1):
l += [l[i-1] + l[i-2]]
return l
If you want to get the Fibonacci sequence up to n:
def fib(n):
series = [0,1]
if n > 1:
c = 1
while c <= n:
series.append(c)
c = series[-2] + series[-1]
return series
Learning Python just for two months, so please be patient .)
Stumbled at very interesting task - perform arithmetic operations with huge numbers ( n: int, number of digits < 10**6 ), represented as str ( len(str) < 10**6 ). I need to split this string in parts, make some simple arithmetics, and give output back as string. Converting to int and back takes about 0.5 sec:
from time import time
from random import choice
string = ''
digits = [str(i) for i in range(10)]
for _ in range(1, 99999):
string += choice(digits)
start = time()
a, b = string[:50000], string[50000:] # Split in half
a, b = map(int, [a, b])
print('str to int:', time() - start)
# Output: str to int: 0.1092...
c = a + b
start = time()
output = ''.join([str(part) for part in [a, b, c]])
print('int to str:', time() - start)
# Output: int to str: 0.4836...
Problem starts when I need to perform this operation in a loop with hundreds iterations
This is something about reverse hashing-like functions
My simple question is this - what kind of optimisation is possible for this kind of situation?
With my best regards! .)
Vadim, I can't answer your question directly. However, let me point you towards the gmpy2 library. The principal author is one of the outrageously clever people we used to see often here on SO; you could use gmpy2 itself, or have a look at the source code.
>>> from gmpy2 import *
>>> s = '012345678901234'
>>> t = '123456789012345'
>>> N = mpz(s)+mpz(t)
>>> N
mpz(135802467913579)
>>> str(N)
'135802467913579'
I want to append float to list, but I got an error like this:
<ipython-input-47-08d9c3f8f180> in maxEs()
12 Es = lists[0]*0.3862 + lists[1]*0.3091 + lists[2]*0.4884
13 aaa = []
---> 14 Es.append(aaa)
15
AttributeError: 'float' object has no attribute 'append'
I guess I can't append float to list. Can I add floats to list another way?
This is my code:
import math
def maxEs():
for a in range(1, 101):
for b in range(1,101):
for c in range(1,101):
if a+b+c == 100 :
lists = []
lists.append(a*0.01)
lists.append(b*0.01)
lists.append(c*0.01)
Es = lists[0]*0.3862 + lists[1]*0.3091 + lists[2]*0.4884
aaa = []
Es.append(aaa)
I don't know what you want, but you are trying to append a list to a float not the other way round.
Should be
aaa.append(Es)
The other answer already explained the main problem with your code, but there is more:
as already said, it has to be aaa.append(Es) (you did it right for the other list)
speaking of the other list: you don't need it at all; just use the values directly in the formula
aaa is re-initialized and overwritten in each iteration of the loop; you should probably move it to the top
you do not need the inner loop to find c; once you know a and b, you can calculate c so that it satisfies the condition
you can also restrict the loop for b, so the result does not exceed 100
finally, you should probably return some result (the max of aaa maybe?)
We do not know what exactly the code is trying to achieve, but maybe try this:
def maxEs():
aaa = []
for a in range(1, 98 + 1):
for b in range(1, 99-a + 1):
c = 100 - a - b
Es = 0.01 * (a * 0.3862 + b * 0.3091 + c * 0.4884)
aaa.append(Es)
return max(aaa)
def repeated(m, result, a, s, d):
check = True
r = 0
while r <= s - 1:
if result == m - 1:
check = False
return check
result = (result ** 2) % m
r = r + 1
return check
I need to write a primality testing python program to test very large numbers, like at least 100-digit numbers. The code above is part of the code for Miller Rabin deterministic primality test for repeated squaring. It works really slow for large numbers. How can I speed it up? It is for a project. Thanks!
your problem is probably the (result ** 2) % m, use the 3 argument version of pow that do the same but more efficiently because the algorithm use is the Modular exponentiation and that is much better than first doing x**n and then calculate its modulo. this way you are guaranty to never have a number bigger than m while if you do (x**n) % m you can have that x**n is very much bigger than m that may be the cause your problems
Also no need for the check variable and you don't use a.
Also as you go from 0 to s-1, better use range
def repeated(m, result, s, d):
for r in range(s):
if result == m - 1:
return False
result = pow(result, 2, m )
return True
Now for this part of the test
if
you need a, d, s, and n this is how I would do it
def mr_check(n,a,s,d):
result = pow(a,d,n)
if result == 1 :
return False
for r in range(s):
result = pow(result,2,n)
if result == n-1:
return False
return True