Skip to content

Instantly share code, notes, and snippets.

@mbc9news
Created May 20, 2013 04:28
Show Gist options
  • Save mbc9news/5610416 to your computer and use it in GitHub Desktop.
Save mbc9news/5610416 to your computer and use it in GitHub Desktop.
//http://euler.synap.co.kr/prob_detail.php?id=5
//1 ~ 10 사이의 어떤 수로도 나누어 떨어지는 가장 작은 수는 2520입니다.
//그러면 1 ~ 20 사이의 어떤 수로도 나누어 떨어지는 가장 작은 수는 얼마입니까?
import scala.collection.mutable.ListBuffer
class Euler_005 {
def getAbc(num:Int){
var resultMap = Map[Int,Int]()
for(i <- 1 to num){
resultMap = getMaxMap(resultMap, getPalindrome(i))
}
println(getBigInt(resultMap))
}
def getPalindrome(trgNum:Int, curNum:Int=2, curMap:Map[Int,Int]=Map[Int,Int]()):Map[Int,Int] = {
if (trgNum < curNum) return curMap
if (trgNum % curNum equals 0) return getPalindrome(trgNum/curNum, curNum, curMap.updated(curNum, curMap.getOrElse(curNum, 0)+1))
else return getPalindrome(trgNum, curNum+1, curMap)
}
def getMaxMap(orgMap:Map[Int,Int], insertMap:Map[Int,Int]):Map[Int,Int] = {
var tmpOrgMap = orgMap
for(i <- insertMap.keys; if(insertMap.getOrElse(i, 0) > orgMap.getOrElse(i, 0))){
tmpOrgMap = orgMap.updated(i, insertMap.getOrElse(i, 0))
}
return tmpOrgMap
}
def getBigInt(insertMap:Map[Int,Int]):Int = {
var result = 1
for(m <- insertMap.keys){
var tmpN = 1
for(n <- 1 to insertMap.getOrElse(m, 0)) tmpN *= m
result *= tmpN
}
return result
}
}
object Euler_005 extends App {
new Euler_005().getAbc(10)
new Euler_005().getAbc(20)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment