I need to put this equation
P*(1 + r/100n)^nt
into python. Can anyone help me?
I've tried this, but it won't get me right answer
p*(1+r/100*n)**(n*t)
p is 116000
t is 35
r is 4
n is 12
I'm suppose to get $469,309.30 from above values, but the number I get is way too high. Its only been hours since I started to learn programming. I just have no idea what to do.
It might be an order of operations issue where Python is dividing r by 100 first. I would try the following:
p*(1+r/(100*n))**(n*t)
Just follow the PEMDAS rule and you'll be fine man :D.
p = 116000
t = 35
r = 4
n = 12
answer = p*(1+r/(100*n))**(n*t)
print(answer)
out: 469309.29562481085
Try this,
>>> "${:,.2f}".format(p*(1+r/(100*n))**(n*t))
'$469,309.30'
Explanation:
PEMDAS - Rule
Parentheses, Exponentiation, Multiplication, Division, Addition,
Subtraction
You need to follow this rule, while writing math equations in code.
In your case, r was divided by 100 as / comes first then it was multiplied by n.
According to bodmas it will divide r by 100 so you need to use brackets at r/(100*n)
result=p*(1+r/(100*n))**(n*t)
print(result)
output:
469309.29562
Related
Could someone please tell me a method to find power of 2 required to obtain a known value. For example, let's say I need to find the power of two which gives the value 32. In other words, if I know a certain value, how to find the power of two which give that value.
if the given value is 64 how to get the power of two as 8
I have searched whether similar questions have been asked in stackoverflow before, but unfortunately I could not find any. At the moment I have nothing on my hand as an attempt taken, sorry for that.
Any help is greatly appreciated in advance.
import math
print (math.log(64, 2))
print (2**math.log(64, 2))
Output:
6
64
You can use the python math module's log2() function. Here is the code:
import math
print (math.log2(64))
>>> x, n = 0,64
>>> while n! = 1:
x += 1
n >>= 1
>>> x
6
I just started learning how to code, and I've been assigned a problem that I've been stuck on for many hours now and was hoping I could receive some hints at the very least to solve the problem. The main point of this exercise is to practice division and modulus. We can use basic statements, but nothing fancy like conditionals or anything since we haven't gotten to that point.
I need a user to input a # from 1 - 25, and then my program will let them know which unit and row that number is in. I've managed to get the code working for the rows, but I cannot figure out how to get the unit number.
Here's my code:
shelfNumber = int(raw_input('What is the shelf number? '))
row = int(shelfNumber / 5.1) + 1
unit =
I've tried a lot of things for unit, but none of them worked out, so I left it blank. I would appreciate any hints that anyone can give me. Thank you for any help.
Edit: I realized that I should try and at least show which ideas I've tried. If I do a regular modulo with # % 5, that works for everything but the multiples of 5 all the way on the right. I've also tried implementing the row #'s each # has but haven't gotten anywhere with that either. I've also tried something similar by dividing by a decimal, casting it as an int, then using modulo but failed, etc., etc.\
Edit: Sorry, I realized I uploaded the wrong image.
This problem would be easier if everything were countrd from 0 instead of from 1. That is, if the row and unit numbers were 0 to 4 instead of 1 to 5 and if the input value were 0 to 24 instead of 1 to 25.
In that case, we'd just write:
row = shelfNumber / 5
unit = shelfNumber % 5
Since everything starts ftom 1 ("is one-indexed" in the usual jargon), shelfNumber is one bigger than what that formula needs, and we need to make row and unit one bigger than what we computed.
But there's no trouble fixing that:
row = (shelfNumber - 1) / 5 + 1
unit = (shelfNumber - 1) % 5 + 1
In Python 3, you'd need to write // insted of /, and that will work with a reasonably recent Python 2.
I have tried to compute this algorithm with python and it doesn't seem to work:
lt = False
x = 5
g = 2
while lt == False:
if g*g > (x-0.1) and g*g < (5.1):
lt = True
print(g+"IS THE SQUARE ROOT")
else:
g = (g + x/g)/2
print(g)
In the else loop, I printed g to see the outcome of my algorithm in each loop because I was experiencing slow computation previously and wanted to see what the problem was, and now print(g) seems to consistently be returning 2. I'm new to python and the problem is probably staring me in the face but I can't seem to figure it, any help would be much appreciated!
x = float(5)
g = float(2)
Python rounds int in v2.x. Hope this helps.
You are getting 2 because python is rounding the numbers because you are using integers, you need to use floats like so:
x = float(5)
g = float(2)
Now:
>>g = (g + x/g)/2
>>print(g)
2.25
Swap the else: clause and block and the print() call.
The problem is your mix of tabs and spaces. Python expects you to be consistent, and it is preferred to use four spaces for indents. Tabs are a different character altogether, and do not always appear the same width. In some environments they are 4 characters wide. Python interprets them as 8 characters wide.
These two lines are using spaces:
if g*g > (x-0.1) and g*g < (5.1):
lt = True
All other lines are using tabs.
The problem is that python rounds integers. You need to enter set x and g as floats instead
x = float(5)
g = float(2)
This should work, good luck! :)
This works in python 2.x but I don't know what version you are using so this is the best answer I can provide.
ok so I am feeling a little stupid for not knowing this, but a coworker asked so I am asking here: I have written a python algorithm that solves his problem. given x > 0 add all numbers together from 1 to x.
def intsum(x):
if x > 0:
return x + intsum(x - 1)
else:
return 0
intsum(10)
55
first what is this type of equation is this and what is the correct way to get this answer as it is clearly easier using some other method?
This is recursion, though for some reason you're labeling it like it's factorial.
In any case, the sum from 1 to n is also simply:
n * ( n + 1 ) / 2
(You can special case it for negative values if you like.)
Transforming recursively-defined sequences of integers into ones that can be expressed in a closed form is a fascinating part of discrete mathematics -- I heartily recommend Concrete Mathematics: A Foundation for Computer Science, by Ronald Graham, Donald Knuth, and Oren Patashnik (see. e.g. the wikipedia entry about it).
However, the specific sequence you show, fac(x) = fac(x - 1) + x, according to a famous anecdote, was solved by Gauss when he was a child in first grade -- the teacher had given the pupils the taksk of summing numbers from 1 to 100 to keep them quet for a while, but two minutes later there was young Gauss with the answer, 5050, and the explanation: "I noticed that I can sum the first, 1, and the last, 100, that's 101; and the second, 2, and the next-to-last, 99, and that's again 101; and clearly that repeats 50 times, so, 50 times 101, 5050". Not rigorous as proofs go, but quite correct and appropriate for a 6-years-old;-).
In the same way (plus really elementary algebra) you can see that the general case is, as many have already said, (N * (N+1)) / 2 (the product is always even, since one of the numbers must be odd and one even; so the division by two will always produce an integer, as desired, with no remainder).
Here is how to prove the closed form for an arithmetic progression
S = 1 + 2 + ... + (n-1) + n
S = n + (n-1) + ... + 2 + 1
2S = (n+1) + (n+1) + ... + (n+1) + (n+1)
^ you'll note that there are n terms there.
2S = n(n+1)
S = n(n+1)/2
I'm not allowed to comment yet so I'll just add that you'll want to be careful in using range() as it's 0 base. You'll need to use range(n+1) to get the desired effect.
Sorry for the duplication...
sum(range(10)) != 55
sum(range(11)) == 55
OP has asked, in a comment, for a link to the story about Gauss as a schoolchild.
He may want to check out this fascinating article by Brian Hayes. It not only rather convincingly suggests that the Gauss story may be a modern fabrication, but outlines how it would be rather difficult not to see the patterns involved in summing the numbers from 1 to 100. That in fact the only way to miss these patterns would be to solve the problem by writing a program.
The article also talks about different ways to sum arithmetic progressions, which is at the heart of OP's question. There is also an ad-free version here.
Larry is very correct with his formula, and its the fastest way to calculate the sum of all integers up to n.
But for completeness, there are built-in Python functions, that perform what you have done, on lists with arbitrary elements. E.g.
sum()
>>> sum(range(11))
55
>>> sum([2,4,6])
12
or more general, reduce()
>>> import operator
>>> reduce(operator.add, range(11))
55
Consider that N+1, N-1+2, N-2+3, and so on all add up to the same number, and there are approximately N/2 instances like that (exactly N/2 if N is even).
What you have there is called arithmetic sequence and as suggested, you can compute it directly without overhead which might result from the recursion.
And I would say this is a homework despite what you say.
What is the difference between the following statements?
Statement 1:
var=2**2*3
Statement 2:
var2=2*2*3
I see no difference.
This raises the following question.
Why is Statement 1 used if we can use Statement 2?
Try:
2**3*2
and
2*3*2
to see the difference.
** is the operator for "power of". In your particular operation, 2 to the power of 2 yields the same as 2 times 2.
Double stars (**) are exponentiation. So "2 times 2" and "2 to the power 2" are the same. Change the numbers and you'll see a difference.
2**2 means 2 squared (2^2)
2*2 mean 2 times 2 (2x2)
In this case they happen to have the same value, but...
3**3*4 != 3*3*4
For visual learners.........................
To specifically answer your question Why is the code1 used if we can use code2? I might suggest that the programmer was thinking in a mathematically broader sense. Specifically, perhaps the broader equation is a power equation, and the fact that both first numbers are "2" is more coincidence than mathematical reality. I'd want to make sure that the broader context of the code supports it being var = x * x * y in all cases, rather than in this specific case alone. This could get you in big trouble if x is anything but 2.
2**2 = 2 power-of 2
2*2 = 2 times 2
The ** operator in Python is really "power;" that is, 2**3 = 8.
The top one is a "power" operator, so in this case it is the same as 2 * 2 equal to is 2 to the power of 2. If you put a 3 in the middle position, you will see a difference.
A double asterisk means to the power of. A single asterisk means multiplied by. 22 is the same as 2x2 which is why both answers came out as 4.
Power has more precedence than multiply, so:
2**2*3 = (2^2)*3
2*2*3 = 2*2*3