#im using a midpoint reimann sum casue the asnwer will be natural numbers
#8/n*sum(k=1 to n, sqrt(k*(n-k))
# n
# 8 _____
#_____ \ ___________
# n / \|k ( n - k )
# _____
# k = 1
from logic import add,subtract,divide,multiply
def binary_search(f, a, b, tol=1e-7):
if multiply(f(a),f(b)) >= 0: raise ValueError()
while abs(subtract(b,a) > tol:
mid = divide(add(a,b),2)
a, b = (mid, b) if multiply(f(mid),f(a)) < 0 else (a, mid)
return divide(add(a,b),2)
def summation(start, end, function):
return sum(function(index) for index in range(start, end + 1))
def func_sum(n):
def ans(k):
def riemann(x):
multiply(x,x)-multiply(k,subtract(n,k))
binary_search(riemann,1,multiply(k,subtract(n,k)))
summation(1, n, rei
#🔒 im trying to define a var inside a function that doesnt explicitly return the var
66 messages · Page 1 of 1 (latest)
@wanton vine
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
hello!
yo
what is your problem?
def ans(k):
def riemann(x):
multiply(x,x)-multiply(k,subtract(n,k))
binary_search(riemann,1,multiply(k,subtract(n,k)))
summation(1, n, riemann
thisline
i need to use a var in the reimann function without returning it inside the func
i assume the indentation is discord's fault
yes
ooo this?
ik the answer
buttttt
i dont wanna spoon feed
google "python anynomous functions"
that could help :)
you don't need to worry about spoonfeeding this guy
ima just do a global var
what?
. . . . .
I was saying you're more than capable of taking suggestions and learning from it.
thanks
binary_search(lambda: riemann(your_arguments_here),1,multiply(k,subtract(n,k)))
lambda is an anynomous function, so you basically create a function to call a function
casue i saw someone say i should just define it instead of lambda
Wouldn't you just need to return multiply(x,x)-multiply(k,subtract(n,k)) from your riemann func?
do you know lambdas?
it behaves a lot like normal functions, but types etc are less explicit
IMO it can make code messy if the lambda gets to be too complex
lambda a, b: a | b returns a union of a and b which can be provided as keyword or positional arguments
i rightfully disagree
use lambda
this is so confusing im trying to use binary search to solve a sqrt in sokme code and constantly increment a value each time its run
brb
"rightfully"? if you say so lol
note that it was my opinion. I wasn't stating a hard fact
if you just simply use a lambda to make a function to call a function is fine
mainly for Callable parameters of lib's functions
like tkinter
hold on lemme make an example
i don't need an example. I'm well aware of good use cases for lambda
bro
i'm also quite aware of cases where lambdas make code messy
look at esoteric python
that's all i'm saying
it can make code messy and people do abuse the rediculous look of pure lambda code
import tkinter as tk
window = tk.Tk()
button = tk.Button(window, text="say hello", command=lambda: print("Hello world!"))
button.pack()
window.mainloop()
``` something like this could occur
i wanted to try lambda: window.destroy() but it doesnt need any arguments
its doneeee
#im using a midpoint reimann sum casue the asnwer will be natural numbers
#8/n*sum(k=1 to n, sqrt(k*(n-k))
# n
# 8 _____
#_____ \ ___________
# n / \|k ( n - k )
# _____
# k = 1
from logic import add, subtract, divide, multiply
def binary_search(f, a, b, tol=1e-7):
if multiply(f(a), f(b)) >= 0:
raise ValueError("Function must have different signs at the endpoints.")
while abs(subtract(b, a)) > tol:
mid = divide(add(a, b), 2)
if multiply(f(mid), f(a)) < 0:
b = mid
else:
a = mid
return divide(add(a, b), 2)
def summation(start, end, function):
return sum(function(index) for index in range(start, end + 1))
def func_sum(n):
return summation(1, n, lambda k: sqrt(multiply(k, subtract(n, k))))
def ans(n):
def riemann(k):
return multiply(8, divide(func_sum(n), n))
return binary_search(lambda x: subtract(riemann(x), x), 0, n)
# Example usage:
n = 10 # Replace with your desired value of n
result = ans(n)
print("Approximate square root:", result)
gg
finally
:D
nice
wai nvm
i forgot to imple,ment sqrt
ok closed i guess
wai nvm
#im using a midpoint reimann sum casue the asnwer will be natural numbers
#8/n*sum(k=1 to n, sqrt(k*(n-k))
# n
# 8 _____
#_____ \ ___________
# n / \|k ( n - k )
# _____
# k = 1
from logic import add, subtract, divide, multiply
sqrt = binart_search(lambda x: x**2 - multiply(k, subtract(n, k))
def binary_search(f, a, b, tol=1e-7):
if multiply(f(a), f(b)) >= 0:
raise ValueError("Function must have different signs at the endpoints.")
while abs(subtract(b, a)) > tol:
mid = divide(add(a, b), 2)
if multiply(f(mid), f(a)) < 0:
b = mid
else:
a = mid
return divide(add(a, b), 2)
def summation(start, end, function):
return sum(function(index) for index in range(start, end + 1))
def func_sum(n):
return summation(1, n, lambda k: sqrt(multiply(k, subtract(n, k))))
def ans(n):
def riemann(k):
return multiply(8, divide(func_sum(n), n))
return binary_search(lambda x: subtract(riemann(x), x), 0, n)
# Example usage:
n = 10 # Replace with your desired value of n
result = ans(n)
print("Approximate square root:", result)
i have to access the "k" and "n" var in the "sqrt =" line so i can properly change what the lambda function outputs but if i change it my algorithm doesnt work
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.