This led to playing around with Scala's Sequence comprehension's a bit. Here's my solution:
object Euler21 {
def sumOfDivisors(number: Int): Int = {
List.range(1, number).filter{i => (number % i) == 0}.foldLeft(0){(a,b) => a+b}
}
def main(args: Array[String]): Unit = {
(for (i <- 0 until 10000; d_i = sumOfDivisors(i);
if(d_i > i && sumOfDivisors(d_i) == i))
yield (i + d_i)).foldLeft(0){(a,b) => a+b};
}
}
I like the brevity of this solution. The function sumOfDivisors first filters out all divisors of a given number and sums the resulting list using the foldLeft function. The main function first computes this sum for each number in the range 1 till 10000. The resulting number, d_i is used for further processing only if it's larger than the original number, this is to avoid double results of the same pair of amicable numbers twice (i.e if we have processed the number 220 and found 284 as its amicable pair, we don't have to go through the same exercise again if we process 284 next in our list). If this holds, we calculate the sum of divisors of the number d_i. If that's equal to our original number, we have a match, so we add the sum of the two numbers it to our output. The output is Scala's Projection trait, so we can obtain the desired sum by adding up all numbers using a foldLeft function again. Incidentally, you may want to check whether the generated sequence of numbers is actually correct. In this case, instead of doing the summing, use this line of code:
for (i <- 0 until 100000; di = sumOfDivisors(i); if(di > i && sumOfDivisors(di) == i) ) yield (Pair(i, di));
When executed this yields the following output:
scala> for (i <- 0 until 10000; di = sumOfDivisors(i); if(di > i && sumOfDivisors(di) == i) ) yield (Pair(i, di));
res2: Seq.Projection[(Int, Int)] = RangeMFM((220,284), (1184,1210), (2620,2924), (5020,5564), (6232,6368))
給你一個鼓勵.................................................
ReplyDeleteAlways keep your words soft and sweet, just in case you have to eat them.............................................
ReplyDelete謝謝你的分享,真的太用心了≧﹏≦........................................
ReplyDelete淫女情慾情色做愛限制級波霸口交18禁貼圖寫真視訊援交露點爆乳潮吹裸體裸照裸女愛愛無碼尋夢視訊聊天a漫a片a圖一夜情一葉情人妻激情情色寫真美女自拍辣妹自拍正妹自拍美女走光
ReplyDelete憂能傷身,保重哦!........................................
ReplyDelete成人色情圖片激情聊天室火辣美眉520sex赤裸美女成人自拍貼圖18限性影片觀賞av色情影音聊天手淫激情成人聊天室成人色情圖片網sex520自拍走光照片淫慾18禁成人影音聊天美女台灣性網限制級女生手淫成人聊天春宮裙底風光情趣丁字褲極度震撼情色論壇露點成人視訊聊天avlive show愛愛明星露點台灣色情網站自慰少婦成人聊天網美女視訊成人頻道人妻熟女蕩婦一對多激情成人色情聊天室av成人上空秀巨奶視訊網愛聊天室一夜正妹色情貼片一對多免費美女視訊粉紅乳頭作愛影片美女聊天sexy成人色情圖片網女人胸部圖片視訊色情情色網站性關係辣妹聊天室大奶子
ReplyDelete不勞而獲的事情,並沒有價值......................................................
ReplyDeleteMore haste, less speed.........................................
ReplyDeleteut聊天77p2p85cc85st85街視訊視訊聊天ava片a片下載成人情色色情影音視訊聊天洪爺影城洪爺免費視訊免費a片免費一對多utsogo論壇ut聊天室成人片免費看................
ReplyDelete閒來無聊逛逛blog~~跟您打聲招呼~~........................................
ReplyDelete只用微笑說話的人,才能擔當重任。..................................................
ReplyDelete來給你加加油~打打氣!!!更新之餘,也要注意休息哦~~ ..................................................
ReplyDelete要持續更新下去喲!!期待~~..................................................
ReplyDeleteyour english is incredible............................................................
ReplyDeleteIt is never too late to learn. ............................................................
ReplyDelete真是好文呀~~給你拍拍手再加分!!!..................................................
ReplyDelete第一次睇你blog,鐘意! .................................................................
ReplyDelete死亡是悲哀的,但活得不快樂更悲哀。.................................................................
ReplyDelete知識可以傳授,智慧卻不行。每個人必須成為他自己。......................................................................
ReplyDeletePay somebody back in his own coin.................................................................
ReplyDelete卡爾.桑得柏:「除非先有夢,否則一切皆不成。」共勉!............................................................
ReplyDelete如果你批評他人。你就沒有時間付出愛............................................................
ReplyDelete要照顧身體歐~保重..................................................................
ReplyDelete在你一無所有的時候 是誰在陪伴你 他便是你最重要的人............................................................
ReplyDelete期待新的內容 感謝你.................................[/url]...............
ReplyDelete再度來訪@v@ 謝謝分享囉~ .................................[/url]...............
ReplyDelete看看文章轉換心情,也幫你加個油哦..................................................................
ReplyDelete做好事,不需要給人知道,雖然只是一件微不足道的事,但我相信,這會帶給我快樂。..................................................
ReplyDeleteA truly happy person is one who can enjoy the scenery on a detour. ............................................................
ReplyDelete死亡是悲哀的,但活得不快樂更悲哀。............................................................
ReplyDelete不會從失敗中找尋教訓的人,成功之路是遙遠的。.......................................................
ReplyDeleteKnowledge is power................................................
ReplyDeletePeople throw stones only at trees with fruit on them.............................................................
ReplyDelete好熱鬧喔 大家踴躍的留言 讓部落格更有活力..................................................
ReplyDelete請繼續發表好文!加油加油再加油!............................................................
ReplyDelete你不能左右天氣,但你可以改變心情.................................................................
ReplyDelete好的部落格就要和好朋友分享--感謝分享..................................................
ReplyDelete