Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hole Idea: Quadratic Formula #939

Open
5cw opened this issue Apr 21, 2023 · 2 comments
Open

Hole Idea: Quadratic Formula #939

5cw opened this issue Apr 21, 2023 · 2 comments
Labels
hole-idea An idea for a new hole. idea

Comments

@5cw
Copy link
Contributor

5cw commented Apr 21, 2023

Description
Given integer coefficients
a b c
of a quadratic equation
ax^2 + bx + c = 0
Output the corresponding root(s) in exact simplified form
[n1[/d1]] [±] [[i][√]n2[/d2]]
where each bracketed element must be omitted if possible

Possibilities/Concerns

  • should there be parens √(n2)? should d2 be included in the sqrt or outside? Might be more aesthetically pleasing as [[√(]n2[/d2][)][i]]
  • could also print solutions comma separated, which cleans up some outputs but makes others worse, could specify cases to use and cases not to?
  • should 0 0 0 => indeterminate and 0 0 x => undefined be cases to consider or left out?
  • could also use input strings of the form ax^2 + bx + c or ax^2 + bx + c = 0 or ax^2 + bx + c = d which would look nice but likely would be unnecessary overhead and more trouble than its worth

Example input
These were all done by hand, let me know if you spot mistakes, i'll start working on a program to automatically generate solutions

1 0 0
0 1 -5
0 3 4
1 0 -1
1 0 1
1 0 -5
-22 -3 7
-2 1 2
3 3 4
0 0 0
0 0  5 

Example output

0
5
-4/3
-1, 1 | ±1
-i, i | ±i
-√5, √5 | ±√5
-7/11, 1/2  | -3/44 ± 25/44  this one is an outlier but does come out uglier with the ±
1/4 - √17/4, 1/4 + √17/4 | 1/4 ± √17/4
-1/2 - i√39/6, -1/2 + i√39/6 | -1/2 ± i√39/6 (-1/2 ± √(13/12)i)? ((-1 ± √(13/3)i)/2)?
indeterminate
undefined

EDIT: idrk what i'm doing i've never coded in go before but i think i should have a valid experimental hole implementation. #940.

I think I could be more clever about generating test cases, right now i'm just generating 200 sets of random coefficients, but it would be good to guarantee every possible case is represented

@5cw 5cw added hole-idea An idea for a new hole. idea labels Apr 21, 2023
5cw added a commit to 5cw/code-golf that referenced this issue Apr 21, 2023
@btnlq
Copy link
Contributor

btnlq commented Apr 22, 2023

  • should 0 0 0 => indeterminate and 0 0 x => undefined be cases to consider or left out?

Another way is to assume a ≠ 0 since bx + c is not a quadratic formula.

JRaspass pushed a commit that referenced this issue Apr 26, 2023
* added quadratic formula experimental hole #939

* implemented JRaspass' requested changes
@nwellnhof
Copy link

nwellnhof commented May 5, 2023

For many languages, a lot of work will be done reducing fractions. Here's another idea: Guarantee that the equations have one or two real solutions. The results should be printed in decimal format with a fractional part of a few fixed digits, maybe as little as two. So the output would be something like:

1.23 -4.56
-0.01 -0.01
3.00
0.00

If there's an exact solution with that many digits, it must be printed. Rounding issues could be handled:

  • Require round toward zero (cut off the decimal expansion).
  • You're allowed to round up or down. This obviously requires a bit of extra work when verifying solutions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hole-idea An idea for a new hole. idea
Projects
None yet
Development

No branches or pull requests

3 participants