Quadratic Formula in Multiple Languages

Concepts

Math functions:  square root, power

Printing two (and only two) decimal places when possible

Constraints

Doesn’t handle imaginary numbers

Limited to languages with compilers and interpreters available in Ubuntu Software Center

bash script

# vi quadratic.sh

#!/bin/bash

function quadraticFormula {
  a=$1
  b=$2
  c=$3

  fDiscriminant=$((  (b ** 2) - (4 * a * c)  ))
  fRoot1=`echo "scale=2;(-($b) + sqrt($fDiscriminant)) / (2 * $a)" | bc -l`
  fRoot2=`echo "scale=2;(-($b) - sqrt($fDiscriminant)) / (2 * $a)" | bc -l`

  echo "x=$fRoot1, x=$fRoot2"
}

quadraticFormula 1 -3 -4
quadraticFormula 1 0 -4
quadraticFormula 6 11 -35
quadraticFormula 1 -7 0

# chmod u+x quadratic.sh
# ./quadratic.sh

Note:  Doesn’t round up last decimal;  Doesn’t print decimal point for zero

C

# vi quadratic.c

#include <stdio.h>
#include <math.h>

void quadraticFormula(int a, int b, int c) {
  double fRoot1;
  double fRoot2;

  fRoot1 = (-b + sqrt(pow(b, 2) - (4 * a * c)) ) / (2 * a);
  fRoot2 = (-b - sqrt(pow(b, 2) - (4 * a * c)) ) / (2 * a);

  printf("x=%.2f, x=%.2f\n", fRoot1, fRoot2);
}

int main(void) {
  quadraticFormula(1, -3, -4);
  quadraticFormula(1, 0, -4);
  quadraticFormula(6, 11, -35);
  quadraticFormula(1, -7, 0);

  return 0;
}

# gcc quadratic.c -lm -o quadratic
# ./quadratic

C++

# vi quadratic.cpp

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void quadraticFormula(int a, int b, int c) {
  float fRoot1;
  float fRoot2;

  fRoot1 = (-b + sqrt(pow(b, 2) - (4 * a * c)) ) / (2 * a);
  fRoot2 = (-b - sqrt(pow(b, 2) - (4 * a * c)) ) / (2 * a);

  cout << fixed << setprecision(2);
  cout << "x=" << fRoot1 << ", x=" << fRoot2 << endl;

}

int main(void) {
  quadraticFormula(1, -3, -4);
  quadraticFormula(1, 0, -4);
  quadraticFormula(6, 11, -35);
  quadraticFormula(1, -7, 0);

  return 0;
}

# g++ quadratic.cpp -o quadratic
# ./quadratic

C Sharp

# vi Quadratic.cs

using System;

public class Quadratic {

  void quadraticFormula(int a, int b, int c) {
    float fRoot1;
    float fRoot2;

    fRoot1 = (float) (-b + Math.Sqrt((Math.Pow(b, 2)) - (4 * a * c)))
                   / (2 * a);
    fRoot2 = (float) (-b - Math.Sqrt((Math.Pow(b, 2)) - (4 * a * c)))
                   / (2 * a);

    System.Console.WriteLine("x=" + fRoot1.ToString("0.00") + ", x=" +
                  fRoot2.ToString("0.00"));
  }

  public static void Main() {
    Quadratic q = new Quadratic();
    q.quadraticFormula(1, -3, -4);
    q.quadraticFormula(1, 0, -4);
    q.quadraticFormula(6, 11, -35);
    q.quadraticFormula(1, -7, 0);
  }
}

# gmcs Quadratic.cs
# mono Quadratic.exe

Forth

# vi quadratic.f

variable a
variable b
variable c

variable fDiscriminant

variable fRoot1
variable fRoot2

: quadraticFormula
4e a f@ f* c f@ f* -1e f* fDiscriminant f!
b f@ 2e f** fDiscriminant f@ f+ fDiscriminant f! 

b f@ -1e f*  fDiscriminant f@ fsqrt f+  fRoot1 f!
fRoot1 f@ 2e a f@ f* f/ fRoot1 f!

b f@ -1e f*  fDiscriminant f@ fsqrt f-  fRoot2 f!
fRoot2 f@ 2e a f@ f* f/ fRoot2 f!

." x="
fRoot1 f@ f.

." , x="
fRoot2 f@ f.

;

1e a f!
-3e b f!
-4e c f!
quadraticFormula cr

1e a f!
0e b f!
-4e c f!
quadraticFormula cr

6e a f!
11e b f!
-35e c f!
quadraticFormula cr

1e a f!
-7e b f!
0e c f!
quadraticFormula cr

# gforth quadratic.f

Note:  Doesn’t format to two decimal places

FORTRAN 77

# vi quadratic.f

      program quadratic
      call quadraticFormula(1.0, -3.0, -4.0)
      call quadraticFormula(1.0, 0.0, -4.0)
      call quadraticFormula(6.0, 11.0, -35.0)
      call quadraticFormula(1.0, -7.0, 0.0)
      stop
      end

      subroutine quadraticFormula(a, b, c)
      real a, b, c
      real fRoot1, fRoot2

      fRoot1 = (-b + sqrt((b ** 2) - (4 * a * c)) ) / (2 * a)
      fRoot2 = (-b - sqrt((b ** 2) - (4 * a * c)) ) / (2 * a) 

      write(*,100) fRoot1, fRoot2
 100  format ('x=', F0.2, ', x=', F0.2)

      end

# gfortran quadratic.f -o quadratic
# ./quadratic

Java

# vi Quadratic.java

import java.text.DecimalFormat;

public class Quadratic {

  public Quadratic() {

  }

  public void quadraticFormula(int a, int b, int c) {
    float fRoot1;
    float fRoot2;

    fRoot1 = (float) (-b + Math.sqrt( Math.pow(b, 2) - (4 * a * c)) ) /
                     (2 * a);
    fRoot2 = (float) (-b - Math.sqrt( Math.pow(b, 2) - (4 * a * c)) ) /
                     (2 * a);

    DecimalFormat df = new DecimalFormat("0.00");
    System.out.println("x=" + df.format(fRoot1) + ", x=" + 
      df.format(fRoot2));
  }

  public static void main(String args[]) {
    Quadratic q = new Quadratic();

    q.quadraticFormula(1, -3, -4);
    q.quadraticFormula(1, 0, -4);
    q.quadraticFormula(6, 11, -35);
    q.quadraticFormula(1, -7, 0);
  }
}

# javac Quadratic.java
# java Quadratic

Javascript (Rhino)

# vi quadratic.js

function quadraticFormula(a, b, c) {
  var fRoot1
  var fRoot2

  fRoot1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a)
  fRoot2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a)
  print("x=" + fRoot1.toFixed(2) + ", x=" + fRoot2.toFixed(2))
}

quadraticFormula(1, -3, -4)
quadraticFormula(1, 0, -4)
quadraticFormula(6, 11, -35)
quadraticFormula(1, -7, 0)

# js -f quadratic.js

Common Lisp (GNU)

# vi quadratic.lisp

(defun quadraticFormula(a b c)
  (let (
         (fRoot1 (/
                    (+ (* -1 b)
                      (sqrt
                        (- (expt b 2) (* 4 a c ) )
                      )
                    )
                    (* 2 a)
                 )
         )
         (fRoot2 (/
                    (- (* -1 b)
                      (sqrt
                        (- (expt b 2) (* 4 a c ) )
                      )
                    )
                    (* 2 a)
                 )
         )
       )
    (format t "x=~,2f" fRoot1)
    (format t ", x=~,2f~%" fRoot2)
  )
)

(quadraticFormula 1 -3 -4)
(quadraticFormula 1 0 -4)
(quadraticFormula 6 11 -35)
(quadraticFormula 1 -7 0)

# gcl -load quadratic.lisp

Lua

# vi quadratic.lua

function quadraticFormula(a, b, c)
  fRoot1 = (-b + math.sqrt(math.pow(b, 2) - (4 * a * c))) / (2 * a)
  fRoot2 = (-b - math.sqrt(math.pow(b, 2) - (4 * a * c))) / (2 * a)

  print(string.format("x=%.2f, x=%.2f", fRoot1, fRoot2))
end

quadraticFormula(1, -3, -4)
quadraticFormula(1, 0, -4)
quadraticFormula(6, 11, -35)
quadraticFormula(1, -7, 0)

# lua quadratic.lua

Objective C

# vi Quadratic.m

#import <objc/objc.h>
#import <objc/Object.h>
#import <stdio.h>
#import <math.h>

@interface Quadratic : Object {
}

-(void) quadraticFormula:(int)a  val:(int)b val:(int)c;
@end

@implementation Quadratic : Object

-(void) quadraticFormula:(int)a  val:(int)b val:(int)c {
  double fRoot1;
  double fRoot2;

  fRoot1 = (-b + sqrt(pow(b, 2) - (4 * a * c)) ) / (2 * a);
  fRoot2 = (-b - sqrt(pow(b, 2) - (4 * a * c)) ) / (2 * a);

  printf("x=%.2f, x=%.2f\n", fRoot1, fRoot2);
}

@end

int main(int argc, const char *argv[] ) {
  Quadratic *q = [Quadratic new];

  [q quadraticFormula:1 val:-3 val:-4];
  [q quadraticFormula:1 val:0 val:-4];
  [q quadraticFormula:6 val:11 val:-35];
  [q quadraticFormula:1 val:-7 val:0];
  return 0;
}

# gcc -x objective-c -o quadratic Quadratic.m -lobjc -lm
# ./quadratic

Perl

# vi quadratic.pl

sub quadraticFormula {
  $a = $_[0];
  $b = $_[1];
  $c = $_[2];

  $fRoot1 = (-$b + sqrt(($b ** 2) - (4 * $a * $c)) ) / (2 * $a);
  $fRoot2 = (-$b - sqrt(($b ** 2) - (4 * $a * $c)) ) / (2 * $a);

  printf "x=%.2f, x=%.2f\n", $fRoot1, $fRoot2;

}

quadraticFormula(1, -3, -4);
quadraticFormula(1, 0, -4);
quadraticFormula(6, 11, -35);
quadraticFormula(1, -7, 0);

#perl quadratic.pl

PHP

# vi quadratic.php

<?php

  function quadraticFormula($a, $b, $c) {
    $fRoot1 = (-$b + sqrt( pow($b, 2) - (4 * $a * $c))) / (2 * $a);
    $fRoot2 = (-$b - sqrt( pow($b, 2) - (4 * $a * $c))) / (2 * $a);

    echo "x=" . number_format($fRoot1, 2) . ", x=" .
      number_format($fRoot2, 2) . "\n";
  }

  quadraticFormula(1, -3, -4);
  quadraticFormula(1, 0, -4);
  quadraticFormula(6, 11, -35);
  quadraticFormula(1, -7, 0);

?>

# php quadratic.php

Python

# vi quadratic.py

import math

def quadraticFormula(a, b, c):
  fRoot1 = (-b + math.sqrt((b ** 2) - (4 * a * c)) ) / (2 * a)
  fRoot2 = (-b - math.sqrt((b ** 2) - (4 * a * c)) ) / (2 * a)

  print "x=" + ("%.2f" % fRoot1) + ", x=" + ("%.2f" % fRoot2)

quadraticFormula(1, -3, -4)
quadraticFormula(1, 0, -4)
quadraticFormula(6, 11, -35)
quadraticFormula(1, -7, 0)

# python quadratic.py

Ruby

# vi quadratic.rb

def quadraticFormula(a, b, c)
  fRoot1 = nil
  fRoot2 = nil

  fRoot1 = (-b + Math.sqrt((b ** 2) - (4 * a * c)) ) / (2 * a)
  fRoot2 = (-b - Math.sqrt((b ** 2) - (4 * a * c)) ) / (2 * a)

  printf("x=%.2f, x=%.2f\n", fRoot1, fRoot2)

end

quadraticFormula(1, -3, -4)
quadraticFormula(1, 0, -4)
quadraticFormula(6, 11, -35)
quadraticFormula(1, -7, 0)

# ruby quadratic.rb

Scala

# vi Quadratic.scala

object Quadratic {

  def quadraticFormula(a: Int, b: Int, c: Int) = {
    var fRoot1 = (-b + Math.sqrt( Math.pow(b, 2) - (4 * a * c) ) ) / (2 * a)
    var fRoot2 = (-b - Math.sqrt( Math.pow(b, 2) - (4 * a * c) ) ) / (2 * a)
    printf("x=%.2f, x=%.2f\n", fRoot1, fRoot2)
  }

  def main(args: Array[String]) {
    this.quadraticFormula(1, -3, -4)
    this.quadraticFormula(1, 0, -4)
    this.quadraticFormula(6, 11, -35)
    this.quadraticFormula(1, -7, 0)
  }
}

# scalac Quadratic.scala
# scala Quadratic

Scheme (MIT/GNU)

#vi quadratic.scm

(define fRoot1)
(define fRoot2)
(define fDiscriminant)

(newline)
(define (quadraticFormula a b c)

  (set! fDiscriminant (expt b 2)  )
  (set! fDiscriminant (- fDiscriminant (* 4 a c) ) )

  (set! fRoot1 (* -1 b) )
  (set! fRoot1 (+ fRoot1 (sqrt fDiscriminant) ) )
  (set! fRoot1 (/ fRoot1 (* 2 a)) )
  (display "x=")
  (display fRoot1)

  (set! fRoot2 (* -1 b) )
  (set! fRoot2 (- fRoot2 (sqrt fDiscriminant) ) )
  (set! fRoot2 (/ fRoot2 (* 2 a)) )
  (display ", x=")(display fRoot2)(newline)

)

(quadraticFormula 1 -3 -4)
(quadraticFormula 1 0 -4)
(quadraticFormula 6 11 -35)
(quadraticFormula 1 -7 0)

(exit)

# scheme –load quadratic.scm

Note: This code could probably be written more efficiently

SmallTalk (Squeak)

| q |
q := Quadratic new.
q quadraticFormula: 1 and: -3 and: -4.
q quadraticFormula: 1 and: 0 and: -4.
q quadraticFormula: 6 and: 11 and: -35.
q quadraticFormula: 1 and: -7 and: 0.

quadraticFormula: a and: b and: c
  | fRoot1 fRoot2 |

  fRoot1 := ( (-1 * b) + ( (b raisedTo: 2) - (4 * a * c) ) sqrt) / (2 * a).
  fRoot2 := ( (-1 * b) - ( (b raisedTo: 2) - (4 * a * c) ) sqrt) / (2 * a).

  Transcript show: 'x='.
  Transcript show: (fRoot1 roundTo: 0.01).
  Transcript show: ', x='.
  Transcript show: (fRoot2 roundTo: 0.01).
  Transcript cr.

Note:  Must be entered and run (Do It / Alt-D) through the Squeak environment

Tcl

# vi quadratic.tcl

proc quadraticFormula {a b c} {
  set fRoot1 [expr {(-$b + sqrt(pow($b, 2) - (4 * $a * $c))) / (2 * $a)}]
  set fRoot2 [expr {(-$b - sqrt(pow($b, 2) - (4 * $a * $c))) / (2 * $a)}]

  puts [format "x=%.2f, x=%.2f" $fRoot1 $fRoot2]
}

quadraticFormula 1 -3 -4
quadraticFormula 1 0 -4
quadraticFormula 6 11 -35
quadraticFormula 1 -7 0

# tclsh quadratic.tcl

Output

x=4.00, x=-1.00
x=2.00, x=-2.00
x=1.67, x=-3.50
x=7.00, x=0.00
Advertisements

2 Responses to Quadratic Formula in Multiple Languages

  1. usama sohail says:

    i just could’nt understand it:-(

  2. Pingback: Algorithmie en première S – Je suis en pétard « 40tude

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: