Python- Calculate this equation / expression - python

I have tried two different syntax's and when I ran the syntax's I got the mathmatical resolution in both attempts, but my quiz is wanting a specific line of code to get the result. Any idea how I'm S U P P O S E D to get python to calculate this?

This is not really a python problem and more a maths order of operations problem.
In both examples you provide, you are adding the 1 after you divide (5**(1/2)/2).
You want this:
ratio = (1 + 5**(1/2)) / 2

You should try this one. It will help you understand the operator order better.
(1+(5**(0.5)))/2
Hope this helps

Related

Why is this code outputting the wrong answer?

I'm new to both StackOverflow and coding so I appreciate any and all support!
I'm trying to solve for x in this equation: x = (p^4).((15-(4p))-((10(p^2))/(1-((2p).(1-p)))))
I wrote the below Python code in PyCharm in order to calculate 'x'.
When p=60, the output I expect is 0.735729231 but the output I get when I run the code is [-2981888998.72899]
I'd appreciate some guidance on what I'd need to change in order to output the expected value.
Many thanks!
PS: for reference: here is an image showing the equation written in its original format
import numpy as np
from sympy import *
p = 60
x = symbols('x')
eqn = Eq(x,(p**4)*((15-(4*p))-((10*(p**2))/(1-((2*p)*(1-p))))))
sol = solve(eqn,x)
print(sol)
I think there something wrong with your calculation or your equation.
You can do this directly without any packages,
p = 60
print((p**4)*((15-(4*p))-((10*(p**2))/(1-((2*p)*(1-p))))))
You don't need sympy to solve this because all the p variables are on the right side already. x is basically just your answer for subbing in p = 60 and expanding and solving the equation.
And You get the same number -2981888998.72899

How to write a while loop in python

I am really having trouble getting started on this assignment and would really appreciate some help as a newbie!
We need to write a program called PiApproximator that approximates the mathematical constant π by summing a finite number of terms in a series for it.
The series we are using is pi=4-4/3+4/5-4/7 etc..
Since you said you just want to get started on solving this I'll break down the components of the question
While function statement; the loop needs to continue as long as the added term is greater than 1e-6, so you'll need a variable for whatever variable is added for that loop.
You need a counter for the number of loops; both for an output and in order to control whether the term will be added or subtracted from the total (hint: a % is useful here)
You will need a way to change the next number in the series; a good way of doing this would be to link it to the loop counter ie series_num = 4/(3 + 2 * loop)
I've tried to give as much info as possible without straight out giving you the answer but let me know if you need any more help
Your code has the right ideas. One solution would be to make the different parts simpler
# pi ~ + 4/1 - 4/3 + 4/5 - 4/7 ...
pi, x, d = 0, 1, 1
while 4 / d > 1e-6:
pi += 4 / d * x
d += 2
x *= -1
print(f'Approximation of pi is {pi} [in {(d+1) // 2} iterations]')
Output
Approximation of pi is 3.141592153589724 [in 2000001 iterations]

make calculator by spliting input text in python

guys thanks for having me I've got a question already.
What I wanna do is to get sum of the list without for loop after splitting the given text by math symbols which the text as an example ** (1+3) * 3 ** should obtain the math priority for calculation.
my first question is how to get sum and sub and multiply or divide by the list and then how to check the priority and check it first.
# my calc
a = input()
result = a.split('+')
print(sum(result))
sol1: split brackets and mul /dev earlier sum /sub later but I know that split is not the best way!
sol2: make a tree I don t know what it is lol but mind it
it has answered here I know but with no split
Calculator in python
You could use eval (but be aware that it is usually a bad practice, see this answer):
result = eval(input())
If you input a string like (3-8)*4+5/2, the result will be automatically computed using normal priorities: -17.5.

Python multiple series multiplication simplified

Guys I have the following short version of my code.
ab10=10
ab11=78
ab12=68
bc10=91
bc11=73
bc12=54
df10=87
df11=81
df12=90
b1=ab10/df10
b2=ab11/df11
b3=ab12/df12
c1=bc10/df10
c2=bc11/df11
c3=bc12/df12
m1=bc10/ab10
m2=bc11/ab11
m3=bc12/ab12
Isthere shorter way to make such multiplications as I have more and more such variables to calculate by years from 10-12 ?
I tried for i in range (10,12) but not working. In my case variables ab10, ab11 and so on I used other variables. Doing everything manually takes lots of time provided there are more years not limited to 10,11,12 at least 10 years. I will be happy to hear any way to simplify the process.
I would. appreciate any thoughts or codes shares to direct me and make my work efficient.
You could also consider using numpy if the amount of numbers is very high, the numbers are saved as vectors to which you can perform operations:
import numpy as np
ab1 = np.array([10,78,68])
bc1 = np.array([91,73,54])
df1 = np.array([87,81,90])
b = ab1/df1
c = bc1/df1
m = bc1/ab1
You can use dictionary for this kind of problems:
my_dict = {'ab10': 10, 'ab11':78, 'df10':87}
b1 = my_dict.ab10 / my_dict.df10 or my_dict['ab10] / my_dict['df10]

Python: Regular expression for quadratic equations

I am currently trying to do a quadratic equation solver. I searched on the web how people did their version, but all of them implied that the user entered the coefficients, which, although the easiest way, I really hate it.
I want the user to introduce the whole equation, and let the program know which are the coefficients, and calculate the solution. I discovered the concept of regex, and automatically, the re module.
I understand how to implement the quadratic formula to solve the problem, but the problem is that I don't know which function should I use, and how to get the coefficients from the input.
I want the regex to be like:
\d(\sx\s\(\^)\s2/x\^2)(\s\+\s)\dbx(\s\+\s)\d = 0
To find the coefficients in this:
ax^2 + bx + c = 0
I am aware that the regex sucks, because I only started to understand it yesterday, so you can also tell me how to improve that.
EDIT:
Let's clarify what I exactly want.
How to improve the regex that I tried doing above?
What Python function should I use so that I can only have the coefficients?
How can I take the groups and turn them into usable integers, assuming that it doesn't store those groups?
Assumptions: the coefficients are numbers and the variable is x:
(-?\d+)x\^2 ([+-]\d+)x ([+-]\d+)
Now for -3x^2 +7x -44 your first group match will be -3, second group will be +7 and third group will be -44.
Round brackets (()) define a group
? says that what was followed can be matched one or zero times
[+-] defines a character set that will match either a + or a - one time
EDIT: Start to end solution (excuse my rusty python skills, but I hope you get an idea of how to use the regex):
import re
quadratic_equation_matcher = re.compile(r'(-?\d+)x\^2 ([+-]\d+)x ([+-]\d+)')
quadratic_equation = '-3x^2 +7x -44'
matches = quadratic_equation_matcher.match(quadratic_equation)
a = int(matches.group(1))
b = int(matches.group(2))
c = int(matches.group(3))
d = b**2 - 4*a*b
x1 = (-b + d**0.5)/(2*a)
x2 = (-b - d**0.5)/(2*a)
x1 # => -0.75542709911179939
x2 # => 3.0887604324451328
Note that you can make the regex more space permissive like so:
(-? ?\d+) ?x\^2 ([+-] ?\d+) ?x ([+-] ?\d+)
I'd recommend cleaning the input up a but first. Get rid of all the white space, or at least the spaces. Check for an equal sign and see if there's a 0 on one side or the other. If there is you can remove it. If not you have to decide how clever you want to be.
Get it close to a format you want to deal with, in other words. Then you can check if they've entered a valid re.
You also need to decide if you can handle shuffling the order of the terms around and letters other than x. Also whether you want the ^ or ** or just x2.
You probably want to grab each term individually (all the terms between the + or -) and decide what kind of term it is.
In other words there's a lot to do before the re expression.
Incidentally have you seen SymPy

Categories