<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Peter Krantz &#187; Ioke</title>
	<atom:link href="http://www.peterkrantz.com/category/ioke/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.peterkrantz.com</link>
	<description></description>
	<lastBuildDate>Sat, 04 Feb 2012 09:57:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Solving Project Euler Problems With Ioke</title>
		<link>http://www.peterkrantz.com/2009/project-euler-in-ioke/</link>
		<comments>http://www.peterkrantz.com/2009/project-euler-in-ioke/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 18:26:10 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Ioke]]></category>
		<category><![CDATA[euler]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.peterkrantz.com/?p=256</guid>
		<description><![CDATA[For those of you that have been following Ola Bini's work on <a href="http://ioke.org/">Ioke</a>, the dynamic language for the JVM, I am happy to report that the current release 0.1.1 is usable enough to solve <a href="http://projecteuler.net/">Project Euler</a> problems with. I wanted to learn more about Ioke and the best way to learn a new language is to use it on your own. So, here is some example Ioke code for some of the simpler Project Euler problems.]]></description>
			<content:encoded><![CDATA[<p>For those of you that have been following Ola Bini&#8217;s work on <a href="http://ioke.org/">Ioke</a>, the dynamic language for the JVM, I am happy to report that the current release 0.1.1 is usable enough to solve <a href="http://projecteuler.net/">Project Euler</a> problems with. I wanted to learn more about Ioke and the best way to learn a new language is to use it on your own. So, here is some example Ioke code for some of the simpler Project Euler problems.<span id="more-256"></span></p>
<h2>Problem 1</h2>
<p>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.</p>
<p>Brute force version below.</p>
<pre class="brush: jscript; title: ; notranslate">
n = 1
sum = 0
while(n &lt; 1000,
      if(n % 3 == 0 or n % 5 == 0, sum += n)
      n++
)
sum println
</pre>
<p>Or, as a one-liner after a suggestion from Ola:</p>
<p>(1..999) select(n, n % 3 == 0 or n % 5 == 0) fold(+) println</p>
<h2>Problem 2</h2>
<p>Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, &#8230; Find the sum of all the even-valued terms in the sequence which do not exceed four million.</p>
<pre class="brush: plain; title: ; notranslate">
sum = 0
fibs = [0, 1]
current_fib = 0
while(current_fib &lt; 4000000,
    current_fib = fibs[0] + fibs[1]
    if(current_fib % 2 == 0, sum += current_fib)
    fibs[0] = fibs[1]
    fibs[1] = current_fib
)
sum println
</pre>
<h2>Problem 3</h2>
<p>The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143?</p>
<p>Using brute force for this one. Faster and more elegant would have been the Rho algorithm.</p>
<pre class="brush: jscript; title: ; notranslate">
start = 600851475143
num = start
factor = 2

while(factor * factor &lt;= num,
    if(num % factor == 0,
        factor println
        num = num / factor,
        factor++
    )
)

if(num != 1, num println)
</pre>
<h2>Problem 5</h2>
<p>2520 is the smallest number that can be divided by each of the numbers  from 1 to 10 without any remainder. What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?</p>
<p>First, as the numbers 1 to 10 are factors in the numbers 11 to 20 we only need to check divisibility for the the latter. Starting of by setting up methods for greatest common divisor and least common multiple. Brute forcing this by looping over an incremented number will not work in the current version of Ioke (takes a couple of hours). Brute forcing it in Ruby took a couple of seconds.</p>
<pre class="brush: jscript; title: ; notranslate">
gcd = method(a, b,
    if(b == 0, a,
        gcd(b, a % b)
    )
)

lcm = method(a, b,
    (a / gcd(a, b)) * b
)

(11..20) inject(number, n, lcm(number, n)) println
</pre>
<h2>Problem 18</h2>
<p>Find the maximum total from top to bottom of the triangle below. This solution also works for problem 67 (a much bigger triangle).</p>
<p>By moving from bottom to top, calculating each cell&#8217;s maximum sum and replacing the value with it we&#8217;ll end up with the total in the first cell in the triangle.</p>
<pre class="brush: jscript; title: ; notranslate">
triangle = [
[75],
[95, 64],
[17, 47, 82],
[18, 35, 87, 10],
[20, 04, 82, 47, 65],
[19, 01, 23, 75, 03, 34],
[88, 02, 77, 73, 07, 63, 67],
[99, 65, 04, 28, 06, 16, 70, 92],
[41, 41, 26, 56, 83, 40, 80, 70, 33],
[41, 48, 72, 33, 47, 32, 37, 16, 94, 29],
[53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14],
[70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57],
[91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48],
[63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31],
[04, 62, 98, 27, 23, 09, 70, 98, 73, 93, 38, 53, 60, 04, 23],
]

;; We replace cells from the bottom up by finding the max sum for each
;; position in the triangle.

current_row = triangle length - 2

while(current_row &gt; -1,
    pos = 0
    while(pos &lt; triangle[current_row] length,
        triangle[current_row][pos] = triangle[current_row][pos] + \
        ([triangle[current_row + 1][pos], triangle[current_row + 1][pos + 1]] sort)[1]
        pos++
    )

    current_row = current_row - 1
)

triangle[0][0] println
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.peterkrantz.com/2009/project-euler-in-ioke/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: www.peterkrantz.com @ 2012-02-04 11:13:13 -->
