Programming을 배우자

Project Euler - 수학문제를 Programming 알고리즘으로 풀어보는 사이트

dextto™ 2011. 5. 9. 16:13
여러분은 새로운 프로그래밍 언어를 공부를 할 때 어떤 방식으로 하시나요?
회사 동료가 Ruby를 공부하면서 즐겨 찾는다는 사이트가 있어 공유하고자 합니다. 

http://projecteuler.net/



회원가입 후 Problems 메뉴로 갑니다. 337개의 문제들이 있군요.
문제를 풀고 정답을 제출하면 최적화된 해법을 pdf로 다운받을 수 있습니다.
문제 풀이에 대해 다른 사람들과 토론도 할 수 있네요.
자기가 풀었던 방식과는 다른 고수들의 사고방식을 엿보고 자기 것으로 체화할 수 있으면 금상첨화겠군요! 

예제로 1번 문제 하나 퍼왔습니다.

Problem 1

05 October 2001

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.



모범 답안(글자가 흰색입니다. 마우스로 긁어서 확인하세요 ^^)

As you have solved this problem we do not have to explain that all numbers divisible by 3 
and/or by 5 should be counted.
So the first numbers to be added would be:
3, 5, 6, 9, 10, 12, 15 and so on.
 
A simple way to do this  is to go through all numbers from 1 to 999 and test whether they are 
divisible by 3 or by 5. 
This would result in code like:

target=999
sum=0
for  i=1 to target do
if (i mod 3=0) or (i mod 5)=0 then sum:=sum+i
output sum

(In some programming languages the mod operator is written as %)
Simple enough you might say.
But wait a minute: if we had asked to do the same for all numbers less than 1,000,000,000 that 
is going to take quite a while. Perhaps you would like to try out that first (make sure your sum 
variable does not overflow).

To get a more efficient solution you could also calculate the sum of the numbers less 
than1000 that are divisible by 3, plus the sum of the numbers less than1000 that are divisible 
by 5. But as you have summed numbers divisible by 15 twice you would have to subtract the 
sum of the numbers divisible by 15.
 
If  we now define a function:
Function SumDivisibleBy(n)
    Details to be filled in
EndFunction

Then  the answer would be 
SumDivisibleBy(3)+SumDivisibleBy(5)-SumDivisibleBy(15)

Let’s look at the details of our function and take as example n=3.
We would have to add:
3+6+9+12+......+999=3*(1+2+3+4+...+333)
For n=5 we would get:
5+10+15+...+995=5*(1+2+....+199)
Now note that 199=995/5 but also 999/5 rounded down to the nearest integer.
In many programming languages there exists a separate operator for that: div or \.
If we now also note that 1+2+3+...+p=½*p*(p+1)  our program becomes:

target=999
Function SumDivisibleBy(n)
  p=target div n
  return n*(p*(p+1)) div 2
EndFunction
Output SumDivisibleBy(3)+SumDivisibleBy(5)-SumDivisibleBy(15) 


copyright Project Euler,further distribution without the consent of the author(s) prohibited
Author: hk 


반응형