2013-02-14 112 views
0

我正在做projecteuler.com問題。問題是這樣的:兩個數組的聯合等於第一個數組?

如果我們在下面列出10是3或 5的倍數的所有自然數,我們得到了3,5,6和9這倍數的總和23.找到 所有倍數3或5的所有倍數低於1000.

我想爲每個3和5創建數組倍數爲1000,並採取它們的聯合,這不會讓我重複(所以我不需要撥打array.uniq)。我已經寫是這樣的:

def get_range(range, step) 
    ret = [] 
    range.step(step) { |i| ret << i } 
    return ret 
end 
p get_range(0..1000, 3) | get_range(0..1000, 5) 

這出來這個結果:

[0,3,6,9,12,15,18,21,24,27,30 ,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,以及美國法典,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180 ,183,186,189,192,195,198,201,204,207,210,213,216,219,222,225,228,231,234,237,240,243,246,249,252,255。在一個實施方案中,所述方法包括: ,258,261,264,267,270,273,276,279,282,285,288,291,294,297,300,303,306,309,312,315,318,321,324,327,330,以及其中之一或其組合,333,336,339,342,345,34 8,351,354,357,360,363,366,369,372,375,378,381,384,387,390,393,396, 423,426,429,432,435,438,441,444,447,450,453,456,459,462,465,468,471,474,477,480,483,486,489,492,495, 498,501,504,507,510,513,516,519,522,525,528,531,534,537,540,543,546,549,552,555,558,561,564,567,570, 573,576,579,582,585,588,591,594,597,600,603,606,609,612,615,618,621,624,627,630,633,636,639,642,645, 648,651,654,657,660,663,666,669,672,675,678,681,684,687,690,693,696,699,702,705,708,711,714,717,720, 723,726,729,732,735,738,741,744,747,750,753,756,759,762,765,768,771,774,777,780,783,786,789,792,795, 798,801,804,807,810,813,816,819,822,825,828,831,834,837,840,843,846,849,852,855,858,861,864,867,870, 873,876,879,882,885,888,891,894,897,900,903,906,909,912,915,918,921,924,927,930,933,936,939,942,945, 94 8,951,954,957,960,963,966,969,972,975,978,981,984,987,990,993,996,999,5,10,20,25,35,40,50,所述方法包括:在步驟(a)和(b)中使用本發明的化合物,其中所述化合物包含選自以下的化合物:55,65,70,80,85,95,100,110,115,125,130,140,​​145,155,160,170,175,185,190,200,205,215,220,230,235,所述方法包括:在步驟(a)中,將所述化合物與所述化合物進行反應,所述化合物包含選自以下的化合物:245,250,260,265,275,280,290,295,305,310,320,325,335,340,350,355,365,370,380,385,395,400,410,415,425,所述方法包括在步驟430,440,440,445,455,460,470,475,485,450,500,505,515,520,530,535,545,550,560,565,575,580, 620,625,635,640,650,655,665,670,680,685,695,700,710,715,725,730,740,745,755,760,770,775,785,790,800, 805,815,820,830,835,845,850,860,865,875,880,890,895,905,910,920,925,935,940,950,955,965,970,980,985, 995]

這是第一陣列。如果我換範圍的順序,然後我得到的5倍數的陣列我想對IRB是這樣的:

[1,3,5] | [3, 5, 7] 
# => [1, 3, 5, 7] 

我失去了一些東西,我只是要瘋了,或者我遇到Ruby中的錯誤?

+0

不要輕易聲稱錯誤。你只是一個初學者。從你在這個問題中看到的粗心大意來看,你很可能永遠不會在Ruby中發現真正的bug。 – sawa 2013-02-14 13:01:45

+1

而不是提供從0..1000測試的樣本,爲什麼不能做0 ..10或0..100?冗餘無助於澄清問題,但它確實混淆了這個問題,並讓我們通過了更多不必要的信息。提問時,簡潔是很重要的。此外,在線編程挑戰時的想法是讓你自己解決它們,而不是尋求解決它們的幫助。 – 2013-02-14 14:38:37

回答

2

你的陣列是正確的。它包含3 的倍數。5.看最後。它只是沒有排序。

+0

Doh,我覺得自己像個白癡。謝謝。 /捂臉 – 2013-02-14 12:18:29

相關問題