Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The OutOfMemoryError happens indenpendently from the fact that you use Stream. As Rex Kerr mentioned above, Stream -- unlike Iterator -- stores everything in memory. The difference with List is that the elements of Stream are calculated lazily, but once you reach 10000000, there will be 10000000 elements, just like List.</p> <p>Try with <code>new Array[Int](10000000)</code>, you will have the same problem.</p> <p>To calculate the fibonacci number as above you may want to use different approach. You can take into account the fact that you only need to have two numbers, instead of the whole fibonacci numbers discovered so far.</p> <p>For example:</p> <pre><code>scala&gt; def fib(i:Long,j:Long): Iterator[Long] = Iterator(i) ++ fib(j, i + j) fib: (i: Long,j: Long)Iterator[Long] </code></pre> <p>And to get, for example, the index of the first fibonacci number exceeding 1000000:</p> <pre><code>scala&gt; fib(1, 1).indexWhere(_ &gt; 1000000) res12: Int = 30 </code></pre> <p><em>Edit: I added the following lines to cope with the StackOverflow</em></p> <p>If you really want to work with 1 millionth fibonacci number, the iterator definition above will not work either for StackOverflowError. The following is the best I have in mind at the moment:</p> <pre><code> class FibIterator extends Iterator[BigDecimal] { var i: BigDecimal = 1 var j: BigDecimal = 1 def next = {val temp = i i = i + j j = temp j } def hasNext = true } scala&gt; new FibIterator().take(1000000).foldLeft(0:BigDecimal)(_ + _) res49: BigDecimal = 82742358764415552005488531917024390424162251704439978804028473661823057748584031 0652444660067860068576582339667553466723534958196114093963106431270812950808725232290398073106383520 9370070837993419439389400053162345760603732435980206131237515815087375786729469542122086546698588361 1918333940290120089979292470743729680266332315132001038214604422938050077278662240891771323175496710 6543809955073045938575199742538064756142664237279428808177636434609546136862690895665103636058513818 5599492335097606599062280930533577747023889877591518250849190138449610994983754112730003192861138966 1418736269315695488126272680440194742866966916767696600932919528743675517065891097024715258730309025 7920682881137637647091134870921415447854373518256370737719553266719856028732647721347048627996967... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload