alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (harmonic.scala)

This example Scala source code file (harmonic.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

array, array, decimalformat, double, double, fieldposition, fieldposition, int, int, stringbuffer, text

The Scala harmonic.scala source code

/* ------------------------------------------------------------------ */
/* The Great Computer Language Shootout                               */
/* http://shootout.alioth.debian.org/                                 */
/*                                                                    */
/* Contributed by Anthony Borla                                       */
/* ------------------------------------------------------------------ */

import java.text.DecimalFormat;
import java.text.FieldPosition;

object harmonic
{
  def main(args: Array[String])
  {
    var n = Integer.parseInt(args(0));

    var value = harmonic(n, 0.0);

    val formatter = new DecimalFormat("#.000000000");
    var formattedValue = formatter.format(value, new StringBuffer(), new FieldPosition(0));

    System.out.println(formattedValue);
  }

  final def harmonic(n: Int, a: Double): Double =
    if (n == 0) a else harmonic(n - 1, a + 1.0 / n);
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala harmonic.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.