Search

Pentesting Guide and Notes

Certification Reviews

Writeups

Euler’s Method

Challenge

Naturally, your favorite class of the day is AP Calculus BC and you've recently been learning Euler's Method. Your teacher has had a lot on their plate, so they've just been using the same curve for your homework problems every day, y'(x) = x^2 - 6y^2, y(5) = 2. To simplify matters, you're going to write a program to automate this trivial task for you.

For each input, you will receive a space-separated set of two numbers (each between -10 and 10). The first is your step size and the second is the x-value of the point you need the estimated y-value for (to the nearest tenth and incuding trailing zero if necessary). Your output will be between -1,000 and 1,000.

Sample Input 1:0.8 5.8Sample Output 1:2.8

Sample Input 2:0.9 7.7Sample Output 2:-645.1

Notes:

  • the inputs will be passed in (through stdin) separated by newlines; make sure your output is also separated by newlines
  • the first line of input will contain only one integer representing the number of additional lines of input you will receive

Solution

def main():
    
    number_of_data = int(input())
    
    y_list = []
    
    for i in range(number_of_data):

        data = input().split(" ")
    
        dx = float(data[0])
        xn = float(data[1])
    
        x = 5.0
        y = 2.0
        f = lambda x, y: x**2 - 6 * y**2
    
        number_of_steps = round((xn - x)/dx)
    
        for step in range(number_of_steps):
    
            y = y + f(x, y) * dx
            x += dx
    
        y_list.append(round(y, 1))
    
    for y in y_list:
        print(y)

main()