Resolve quadratic equation

3

0

Challenge:

Write the smallest program (in characters) that resolves quadratic equations i.e. ax² + bx + c = 0

Rules:

Given 3 numbers in R comma-delimited on STDIN: a,b,c (a != 0), print on STDOUT the 2 roots in C, one per line.

R stands for real numbers, C for complex numbers.

Examples:

Input         Output

1,0,0         r1=0     also accepted: r1=(-0-sqrt(0))/2
              r2=0

1,2,1         r1=-1    also accepted: r1=(-2-sqrt(0))/2
              r2=-1

1,1,1         r1=(-1-isqrt(3))/2  also accepted: r1=(-1-sqrt(-3))/2 or r1=-0.5-0.866i
              r2=(-1+isqrt(3))/2

Toto

Posted 2011-11-28T15:54:50.443

Reputation: 909

What does isqrt mean? Are there any constraints on the output format (e.g. canonical forms) or is any expression which resolves to the roots valid? – Peter Taylor – 2011-11-28T16:01:39.887

@Peter Taylor: isqrt stands for i * squareRoot(3). The output have to be on 2 lines as shown in the output column of examples. – Toto – 2011-11-28T16:06:25.837

But would r1=(-2-sqrt(0))/2 be an acceptable output line? How about r1=(-1-sqrt(-3))/2 ? – Peter Taylor – 2011-11-28T17:27:48.077

@Peter Taylor: I see what you mean, yes both output are acceptable. – Toto – 2011-11-28T18:35:39.847

I implemented this using a Casio fx-7000G - http://www.rskey.org/detail.asp?manufacturer=Casio&model=fx-7000G - (not sure the extact model). I remember there being only 32 steps for a program and the version I came up with was 31 (including the data entry).

– Skizz – 2011-11-29T13:47:57.380

Answers

8

R, 19 chars

polyroot(scan(n=3))

or more strictly following the I/O requirements in 58 chars:

r=polyroot(rev(scan(sep=",")));cat("r1=",r[1],"\nr2=",r[2])

edit: reversed coefficients

Ian Fellows

Posted 2011-11-28T15:54:50.443

Reputation: 181

Great, now just format the output correctly too... – Tommy – 2011-12-03T00:16:01.420

cat(paste("r",1:2,"=",polyroot(rev(scan(se=",",qui=T))),sep=""),sep="\n") You need to rev the coefficients (two of the three examples were palindromic, so it wasn't obvious). Also, added quiet=TRUE to scan to suppress "Read 3 items" and shortened all arguments to minimum possible (which was se instead of sep for scan. – Brian Diggs – 2011-12-03T00:33:50.507

6

J, 21

J's got a verb to do exactly that: p. It does complex to complex, but your problem is a subset of that.

echo"0>{:p.|.".1!:1[3

As always with J solutions here, I/O and formatting take up 90% of the solution.

J B

Posted 2011-11-28T15:54:50.443

Reputation: 9 638

4Whoever downvoted this: why? If it's buggy, post a comment. If you have a problem with "challenges" which are solved in 1 character in a language which is used in the real world then downvote the question, not the answer. – Peter Taylor – 2011-11-29T08:09:46.430

@J B: Nice one, +1 – Toto – 2011-11-29T09:05:12.213

It looks really nice, but I don't know if it follow the contest rules (load from stdin and output r1=) – JBernardo – 2011-11-29T14:10:20.287

@JBernardo: it follows the rules strictly as far as STDIN goes. STDOUT is a bit more relaxed, but acceptable IMHO: it assumes interactive J, and outputs in a format different from the example, but that's in line with my interpretation of word "example"'s meaning. – J B – 2011-11-29T16:41:53.817

Oh, I hadn't noticed the poster required one answer per line. That at least I can fix easily. – J B – 2011-11-29T16:43:12.600

There, fixed. Noticed along the way that commas are already a perfectly acceptable vector separator, so that's a lot of useless characters down the drain. \o/ – J B – 2011-11-29T17:09:27.423

Could you perhaps post the output for those of us that don't have J? – Tommy – 2011-12-03T00:15:27.973

@Tommy: J is freely available at jsoftware.com, but I'll edit anyway.

– J B – 2011-12-03T11:35:14.963

4

Python 3, 79 chars

a,b,c=eval(input())
d=(b*b-4*a*c)**.5/2/a
x=-b/2/a
print('r1=',x+d,'\nr2=',x-d)

Python's imaginary unity is j and not i. I used Python 3 because the power operator works also on negative numbers.

BTW, is it really needed to write r1= ... and r2= ... ?

JBernardo

Posted 2011-11-28T15:54:50.443

Reputation: 1 659

+1, It's OK for j instead of i. r1= is part of the challenge. – Toto – 2011-11-29T09:07:31.647

1

Perl, 96 87 Characters

($a,$b,$c)=eval<>;$_=abs($_=$b*$b-4*$a*$c)**.5/2/$a.i x($_<0);$b/=-2*$a;die"r1=$b+$_
r2=$b-$_"

The line break is intentional, as is a space.

Edit: By reformatting output, I can shorten this to:

($a,$b,$c)=eval<>;$_="sqrt(".($b*$b-4*$a*$c)."))/".2*$a;$b*=-1;die"r1=($b+$_
r2=($b-$_"

PhiNotPi

Posted 2011-11-28T15:54:50.443

Reputation: 26 739

1

Perl, 76 characters (+1 command line switch)

perl -pe '($a,$b,$c)=eval;$d=($b/=-$a)**2-4*$c/$a;$_="-+";s!.!r@+=($b$&sqrt($d))/2\n!g'

Replacing the \n with a literal newline allows a further trivial one-character reduction.

Sample input / output:

1,0,0
r1=(0-sqrt(0))/2
r2=(0+sqrt(0))/2
1,2,1
r1=(-2-sqrt(0))/2
r2=(-2+sqrt(0))/2
1,1,1
r1=(-1-sqrt(-3))/2
r2=(-1+sqrt(-3))/2
1,0,-1
r1=(0-sqrt(4))/2
r2=(0+sqrt(4))/2
2,1,1
r1=(-0.5-sqrt(-1.75))/2
r2=(-0.5+sqrt(-1.75))/2

Yeah, it's not exactly Wolfram Alpha, but I believe it should qualify as acceptable output.

Ilmari Karonen

Posted 2011-11-28T15:54:50.443

Reputation: 19 513

0

Perl, 101 chars

First stab:

perl -E 'my($x,$y,$z)=pop=~/\d+/g;printf"r%d=(%+d%ssqrt(%d))/%d\n",++$p,-$y,$_,$y**2-4*$x*$z,2*$x for qw/- +/;' 1,2,1

Zaid

Posted 2011-11-28T15:54:50.443

Reputation: 1 015

The last $x in printf should be $x*2, and last double quote " a single one ' – Toto – 2011-11-29T09:04:09.173

@M42 : Right. Bitten by the Windows "" issue again. – Zaid – 2011-11-29T09:44:07.143

0

D (160 chars)

import std.complex;import std.stdio;void main(){real a,b,c;readf("%f,%f,%f",&a,&b,&c);Complex!real j=sqrt(b*b-4*a*c)/2/a,i=-b/2/a;write("r1=",i+j,"\nr2=",i-j);}

ratchet freak

Posted 2011-11-28T15:54:50.443

Reputation: 1 334