Created
March 2, 2013 02:20
-
-
Save fayimora/5069397 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @author Fayimora | |
* @description 3n + 1 game | |
*/ | |
object ThreeN extends App | |
{ | |
def sequence(num: Int) = { | |
def recurthreeN(xs: Array[Int], n: Int): Array[Int] = n match { | |
case x if n == 1 => xs :+ 1; xs | |
case x if n%2==0 => recurthreeN( xs:+ n/2, n/2) | |
case _ => | |
val t = 3*n + 1 | |
recurthreeN(xs :+ t, t) | |
} | |
recurthreeN(Array(num), num) | |
} | |
def lengthBumpy(num: Int) = sequence(num).length | |
// Tests for fun | |
val ten = sequence(10) | |
val eleven = sequence(11) | |
// Show the content of the collection | |
println(ten mkString ", ") | |
println(eleven mkString ", ") | |
// Use lengthBumpy | |
println(lengthBumpy(10)) | |
println(lengthBumpy(11)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> | |
<title>~/Code/Scala/ThreeN.scala.html</title> | |
<meta name="Generator" content="Vim/7.3"> | |
<meta name="plugin-version" content="vim7.3_v12"> | |
<meta name="syntax" content="scala"> | |
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy="> | |
<meta name="colorscheme" content="solarized"> | |
<style type="text/css"> | |
<!-- | |
pre { white-space: pre-wrap; font-family: monospace; color: #77a5b1; background-color: #002b36; } | |
body { font-family: monospace; color: #77a5b1; background-color: #002b36; } | |
* { font-size: 1em; } | |
.Constant { color: #00bce0; } | |
.Statement { color: #719e07; } | |
.LineNr { color: #004c60; background-color: #002b36; padding-bottom: 1px; font-weight: bold; } | |
.Comment { color: #52737b; font-style: italic; } | |
.Type { color: #b58900; font-weight: bold; } | |
.Special { color: #dc322f; } | |
.Identifier { color: #268bd2; } | |
--> | |
</style> | |
</head> | |
<body> | |
<pre id='vimCodeElement'> | |
<span class="LineNr"> 1 </span><span class="Comment">/*</span> | |
<span class="LineNr"> 2 </span><span class="Comment"> * @author Fayimora</span> | |
<span class="LineNr"> 3 </span><span class="Comment"> * @description 3n + 1 game</span> | |
<span class="LineNr"> 4 </span><span class="Comment"> */</span> | |
<span class="LineNr"> 5 </span><span class="Statement">object</span> <span class="Special">ThreeN</span> <span class="Statement">extends</span> <span class="Special">App</span> | |
<span class="LineNr"> 6 </span>{ | |
<span class="LineNr"> 7 </span> <span class="Statement">def</span> <span class="Identifier">sequence</span>(num:<span class="Type"> Int</span>) = { | |
<span class="LineNr"> 8 </span> <span class="Statement">def</span> <span class="Identifier">recurthreeN</span>(xs:<span class="Type"> Array[Int]</span>, n:<span class="Type"> Int</span>):<span class="Type"> Array[Int]</span> = n <span class="Statement">match</span> { | |
<span class="LineNr"> 9 </span> <span class="Statement">case</span> x <span class="Statement">if</span> n == <span class="Constant">1</span> <span class="Statement">=></span> xs :+ <span class="Constant">1</span>; xs | |
<span class="LineNr">10 </span> <span class="Statement">case</span> x <span class="Statement">if</span> n%<span class="Constant">2</span>==<span class="Constant">0</span> <span class="Statement">=></span> recurthreeN( xs:+ n/<span class="Constant">2</span>, n/<span class="Constant">2</span>) | |
<span class="LineNr">11 </span> <span class="Statement">case</span> <span class="Statement">_</span> <span class="Statement">=></span> | |
<span class="LineNr">12 </span> <span class="Statement">val</span> t = <span class="Constant">3</span>*n + <span class="Constant">1</span> | |
<span class="LineNr">13 </span> recurthreeN(xs :+ t, t) | |
<span class="LineNr">14 </span> } | |
<span class="LineNr">15 </span> recurthreeN(<span class="Special">Array</span>(num), num) | |
<span class="LineNr">16 </span> } | |
<span class="LineNr">17 </span> <span class="Statement">def</span> <span class="Identifier">lengthBumpy</span>(num:<span class="Type"> Int</span>) = sequence(num).length | |
<span class="LineNr">18 </span> | |
<span class="LineNr">19 </span> <span class="Comment">// Tests for fun</span> | |
<span class="LineNr">20 </span> <span class="Statement">val</span> ten = sequence(<span class="Constant">10</span>) | |
<span class="LineNr">21 </span> <span class="Statement">val</span> eleven = sequence(<span class="Constant">11</span>) | |
<span class="LineNr">22 </span> | |
<span class="LineNr">23 </span> <span class="Comment">// Show the content of the collection</span> | |
<span class="LineNr">24 </span> println(ten mkString <span class="Constant">", "</span>) | |
<span class="LineNr">25 </span> println(eleven mkString <span class="Constant">", "</span>) | |
<span class="LineNr">26 </span> | |
<span class="LineNr">27 </span> <span class="Comment">// Use lengthBumpy</span> | |
<span class="LineNr">28 </span> println(lengthBumpy(<span class="Constant">10</span>)) | |
<span class="LineNr">29 </span> println(lengthBumpy(<span class="Constant">11</span>)) | |
<span class="LineNr">30 </span>} | |
</pre> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment