alan dipert RSS

github / twitter / resume / email
Aug
26th
Wed
permalink

FizzBuzz in Scala

The problem:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

A possible solution, with Scala 2.7.5:

(1 to 100) map { x =>   
  (x % 3, x % 5) match {  
    case (0,0) => "FizzBuzz"
    case (0,_) => "Fizz"    
    case (_,0) => "Buzz"    
    case _ => x toString    
  }
} foreach println
Comments (View)
blog comments powered by Disqus