index | submit | rank | book

function1 – Function from points (challenge)

Write a program that given a pair of assignments f(x₁) = y₁ and f(x₂) = y₂, calculates the function f, given by f(x) = ax + b.

Example 1. If f(0) = 2 and f(2) = 3 then f(x) = x/2 + 2.

Example 2. If f(1) = 6 and f(4) = 12 then f(x) = 2x + 4.

Functions on a plane.

Input and output

The input contains a single line with 4 numbers x₁, y₁, x₂ and y₂

where 0 ≤ x₁, y₁, x₂, y₂ ≤ 100

The output should contain a single line with a function description to the standard output in the form f(x) = A.A*x + B.B where A.A and B.B are the coefficients rounded to one decimal place.

Inputs are given so that it is always possible to produce such function.

Example input 1

0 2 2 3

Example output 1

f(x) = 0.5*x + 2.0

Example input 2

1 6 4 12 

Example output 2

f(x) = 2.0*x + 4.0

Scoring

try first: pi

try next: function

index | submit | rank | book

Copyright © 2020-2022 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/function1