<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Project Euler</title>
	<atom:link href="http://aaronstaves.com/2008/01/01/code-test/feed/" rel="self" type="application/rss+xml" />
	<link>http://aaronstaves.com/2008/01/01/code-test/</link>
	<description>Not quite extinct!</description>
	<lastBuildDate>Mon, 12 Apr 2010 15:39:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Avinash</title>
		<link>http://aaronstaves.com/2008/01/01/code-test/comment-page-1/#comment-34</link>
		<dc:creator>Avinash</dc:creator>
		<pubDate>Fri, 18 Apr 2008 15:03:16 +0000</pubDate>
		<guid isPermaLink="false">http://aaronstaves.com/2008/01/01/code-test/#comment-34</guid>
		<description>Problem 1 is a perfect candidate for some Python goodness--if you use a list comprehension, you can elegantly one-liner the solution:

sum([n for n in range(1000) if n % 3 == 0 or n % 5 == 0])</description>
		<content:encoded><![CDATA[<p>Problem 1 is a perfect candidate for some Python goodness&#8211;if you use a list comprehension, you can elegantly one-liner the solution:</p>
<p>sum([n for n in range(1000) if n % 3 == 0 or n % 5 == 0])</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: IrrationalPi</title>
		<link>http://aaronstaves.com/2008/01/01/code-test/comment-page-1/#comment-8</link>
		<dc:creator>IrrationalPi</dc:creator>
		<pubDate>Mon, 14 Jan 2008 01:27:02 +0000</pubDate>
		<guid isPermaLink="false">http://aaronstaves.com/2008/01/01/code-test/#comment-8</guid>
		<description>You guys got me hooked, decided to finally learn Python as well, and used sets:

def add(x,y): return x+y

threes = range(3, 1000, 3)
fives = range(5,1000,5)
both = set(threes + fives)

print reduce(add, both)</description>
		<content:encoded><![CDATA[<p>You guys got me hooked, decided to finally learn Python as well, and used sets:</p>
<p>def add(x,y): return x+y</p>
<p>threes = range(3, 1000, 3)<br />
fives = range(5,1000,5)<br />
both = set(threes + fives)</p>
<p>print reduce(add, both)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MrVince</title>
		<link>http://aaronstaves.com/2008/01/01/code-test/comment-page-1/#comment-6</link>
		<dc:creator>MrVince</dc:creator>
		<pubDate>Thu, 10 Jan 2008 21:31:13 +0000</pubDate>
		<guid isPermaLink="false">http://aaronstaves.com/2008/01/01/code-test/#comment-6</guid>
		<description>Here&#039;s what I did for problem 1...

If you look at the example they gave for looking at sum of multiples of 3 or 5 that are less than 10, it boils down to

multiples of 3 summed = 3 + 6 + 9
multiples of 5 summed = 5

However, I realized there was a pattern when looking at the 3&#039;s.  More specifically

multiples of 3 summed = 3 ( 1 + 2 + 3 )
multiples of 5 summed = 5 ( 1 )

I realized... summation!  In other words

SUM(n) = 1 + 2 + 3 + ... + n = n*(n+1)/2

This cuts out the need for looping.  All you have to do is figure out how many times 3 and 5 go into 10-1, since we are looking for values LESS than 10.  In the case of 3, it goes into 10 three times, and 1 time for 5.  Those &quot;times&quot; give me a limit as to what n should be in the SUM(n).

So if you plug and chug...

5 * SUM(1) + 3 * SUM(3) = 23

However, when I did it for 1000, I tried this little number (remember 1000-1 =  999)

5 * SUM(999/5) + 3 SUM(999/3)

The result was incorrect!  Why did it work for the example with 10 and not with 1000?  I thought about it for a while and realized, I was double counting!  In other words, I was adding multiples of 15 twice!  15 is the least common multiple of 3 and 5, so I was adding 15, 30, 45, etc. twice in the above formula.  

So we gotta cut out the duplicates!

3 * sum(999/3) + 5 * sum(999/5) - &lt;b&gt;15 * sum(999/15)&lt;/b&gt;

Here is my solution in Java, made a little more abstract:

&lt;code&gt;
package com.vince.euler;

public class Problem1 {

	public static void main(String[] args) {
		if (args.length != 3) {
			System.exit(-1);
		}

		int value1 = Integer.parseInt(args[0]);
		int value2 = Integer.parseInt(args[1]);

		int max = Integer.parseInt(args[2]) - 1;
		int lcm = value1 * value2;
		int result = value1 * sum(max / value1) + value2 * sum(max / value2)
				- lcm * sum(max / lcm);

		System.out.println(&quot;result = &quot; + result);
	}

	public static int sum(int n) {
		return n * (n + 1) / 2;
	}

}
&lt;/code&gt;

No loops!</description>
		<content:encoded><![CDATA[<p>Here&#8217;s what I did for problem 1&#8230;</p>
<p>If you look at the example they gave for looking at sum of multiples of 3 or 5 that are less than 10, it boils down to</p>
<p>multiples of 3 summed = 3 + 6 + 9<br />
multiples of 5 summed = 5</p>
<p>However, I realized there was a pattern when looking at the 3&#8217;s.  More specifically</p>
<p>multiples of 3 summed = 3 ( 1 + 2 + 3 )<br />
multiples of 5 summed = 5 ( 1 )</p>
<p>I realized&#8230; summation!  In other words</p>
<p>SUM(n) = 1 + 2 + 3 + &#8230; + n = n*(n+1)/2</p>
<p>This cuts out the need for looping.  All you have to do is figure out how many times 3 and 5 go into 10-1, since we are looking for values LESS than 10.  In the case of 3, it goes into 10 three times, and 1 time for 5.  Those &#8220;times&#8221; give me a limit as to what n should be in the SUM(n).</p>
<p>So if you plug and chug&#8230;</p>
<p>5 * SUM(1) + 3 * SUM(3) = 23</p>
<p>However, when I did it for 1000, I tried this little number (remember 1000-1 =  999)</p>
<p>5 * SUM(999/5) + 3 SUM(999/3)</p>
<p>The result was incorrect!  Why did it work for the example with 10 and not with 1000?  I thought about it for a while and realized, I was double counting!  In other words, I was adding multiples of 15 twice!  15 is the least common multiple of 3 and 5, so I was adding 15, 30, 45, etc. twice in the above formula.  </p>
<p>So we gotta cut out the duplicates!</p>
<p>3 * sum(999/3) + 5 * sum(999/5) &#8211; <b>15 * sum(999/15)</b></p>
<p>Here is my solution in Java, made a little more abstract:</p>
<p><code><br />
package com.vince.euler;</p>
<p>public class Problem1 {</p>
<p>	public static void main(String[] args) {<br />
		if (args.length != 3) {<br />
			System.exit(-1);<br />
		}</p>
<p>		int value1 = Integer.parseInt(args[0]);<br />
		int value2 = Integer.parseInt(args[1]);</p>
<p>		int max = Integer.parseInt(args[2]) - 1;<br />
		int lcm = value1 * value2;<br />
		int result = value1 * sum(max / value1) + value2 * sum(max / value2)<br />
				- lcm * sum(max / lcm);</p>
<p>		System.out.println("result = " + result);<br />
	}</p>
<p>	public static int sum(int n) {<br />
		return n * (n + 1) / 2;<br />
	}</p>
<p>}<br />
</code></p>
<p>No loops!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://aaronstaves.com/2008/01/01/code-test/comment-page-1/#comment-5</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Sat, 05 Jan 2008 07:37:59 +0000</pubDate>
		<guid isPermaLink="false">http://aaronstaves.com/2008/01/01/code-test/#comment-5</guid>
		<description>I showed you mine for problem 3. Instead of going threw the entire loop I pulled out the factors and &quot;reset&quot; the loop.

x = 10
xnew = x
for i in 2 to xnew
  find prime and store; first prime is 2
  x = xnew/i ;reduce by found prime
  i = 1 ; set loop back to 2 

In lisp xnew is now 5 and it will go from 2 to 5. For larger numbers it even grabs more. I now can get all primes and frequency of primes from any number in less than 0.005 seconds.</description>
		<content:encoded><![CDATA[<p>I showed you mine for problem 3. Instead of going threw the entire loop I pulled out the factors and &#8220;reset&#8221; the loop.</p>
<p>x = 10<br />
xnew = x<br />
for i in 2 to xnew<br />
  find prime and store; first prime is 2<br />
  x = xnew/i ;reduce by found prime<br />
  i = 1 ; set loop back to 2 </p>
<p>In lisp xnew is now 5 and it will go from 2 to 5. For larger numbers it even grabs more. I now can get all primes and frequency of primes from any number in less than 0.005 seconds.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Problem 10 - More prime numbers &#187; AaronStaves.com</title>
		<link>http://aaronstaves.com/2008/01/01/code-test/comment-page-1/#comment-4</link>
		<dc:creator>Problem 10 - More prime numbers &#187; AaronStaves.com</dc:creator>
		<pubDate>Thu, 03 Jan 2008 04:55:00 +0000</pubDate>
		<guid isPermaLink="false">http://aaronstaves.com/2008/01/01/code-test/#comment-4</guid>
		<description>[...] a big part of all this Project Euler nonsense includes prime numbers. Problem 10, begs the ever-so-common question Find the sum of all [...]</description>
		<content:encoded><![CDATA[<p>[...] a big part of all this Project Euler nonsense includes prime numbers. Problem 10, begs the ever-so-common question Find the sum of all [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shollamonster</title>
		<link>http://aaronstaves.com/2008/01/01/code-test/comment-page-1/#comment-3</link>
		<dc:creator>shollamonster</dc:creator>
		<pubDate>Wed, 02 Jan 2008 02:44:04 +0000</pubDate>
		<guid isPermaLink="false">http://aaronstaves.com/2008/01/01/code-test/#comment-3</guid>
		<description>for problem 1,  my first instinct was to only compute the numbers themselves, as in something like:

%initialize as:
x=3;
y=5;

while or for, or whatever
x += 3;
y += 5;


although I guess you would need two loops since x and y would reach a million at different rates, obviously, or a single loop with a means of checking x, y to make sure they are under a million and stop adding contributions after that point.  heh, as usual the idea seemed better until 5 seconds ago when I went to type some psuedocode above...

This site looks excellent though.  I am fairly good with C++...at least I was when I last used it 4 years ago, so this will be good for getting back into programming with it.  

The great thing, too, is that you can in theory do these problems over and over in different languages, which I might make a goal to work through these in C++, and then maybe start some scripting languages or something.</description>
		<content:encoded><![CDATA[<p>for problem 1,  my first instinct was to only compute the numbers themselves, as in something like:</p>
<p>%initialize as:<br />
x=3;<br />
y=5;</p>
<p>while or for, or whatever<br />
x += 3;<br />
y += 5;</p>
<p>although I guess you would need two loops since x and y would reach a million at different rates, obviously, or a single loop with a means of checking x, y to make sure they are under a million and stop adding contributions after that point.  heh, as usual the idea seemed better until 5 seconds ago when I went to type some psuedocode above&#8230;</p>
<p>This site looks excellent though.  I am fairly good with C++&#8230;at least I was when I last used it 4 years ago, so this will be good for getting back into programming with it.  </p>
<p>The great thing, too, is that you can in theory do these problems over and over in different languages, which I might make a goal to work through these in C++, and then maybe start some scripting languages or something.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
