Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3403968
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:19:30+00:00 2026-05-18T05:19:30+00:00

I had asked this question on Javaranch , but couldn’t get a response there.

  • 0

I had asked this question on Javaranch, but couldn’t get a response there. So posting it here as well:

I have this particular requirement where the increment in the loop variable is to be done by multiplying it with 5 after each iteration. In Java we could implement it this way:

for(int i=1;i<100;i=i*5){}

In scala I was trying the following code-

var j=1
for(i<-1.to(100).by(scala.math.pow(5,j).toInt))
{
  println(i+" "+j)
  j=j+1
}

But its printing the following output:
1 1
6 2
11 3
16 4
21 5
26 6
31 7
36 8
….
….

Its incrementing by 5 always. So how do I got about actually multiplying the increment by 5 instead of adding it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:19:30+00:00Added an answer on May 18, 2026 at 5:19 am

    Let’s first explain the problem. This code:

    var j=1
    for(i<-1.to(100).by(scala.math.pow(5,j).toInt))
    {
      println(i+" "+j)
      j=j+1
    }
    

    is equivalent to this:

    var j = 1
    val range: Range = Predef.intWrapper(1).to(100)
    val increment: Int = scala.math.pow(5, j).toInt
    val byRange: Range = range.by(increment)
    byRange.foreach {
      println(i+" "+j)
      j=j+1
    }
    

    So, by the time you get to mutate j, increment and byRange have already been computed. And Range is an immutable object — you can’t change it. Even if you produced new ranges while you did the foreach, the object doing the foreach would still be the same.

    Now, to the solution. Simply put, Range is not adequate for your needs. You want a geometric progression, not an arithmetic one. To me (and pretty much everyone else answering, it seems), the natural solution would be to use a Stream or Iterator created with iterate, which computes the next value based on the previous one.

    for(i <- Iterator.iterate(1)(_ * 5) takeWhile (_ < 100)) {
      println(i)
    }
    

    EDIT: About Stream vs Iterator

    Stream and Iterator are very different data structures, that share the property of being non-strict. This property is what enables iterate to even exist, since this method is creating an infinite collection1, from which takeWhile will create a new2 collection which is finite. Let’s see here:

    val s1 = Stream.iterate(1)(_ * 5) // s1 is infinite
    val s2 = s1.takeWhile(_ < 100)    // s2 is finite
    val i1 = Iterator.iterate(1)(_ * 5) // i1 is infinite
    val i2 = i1.takeWhile(_ < 100)      // i2 is finite
    

    These infinite collections are possible because the collection is not pre-computed. On a List, all elements inside the list are actually stored somewhere by the time the list has been created. On the above examples, however, only the first element of each collection is known in advance. All others will only be computed if and when required.

    As I mentioned, though, these are very different collections in other respects. Stream is an immutable data structure. For instance, you can print the contents of s2 as many times as you wish, and it will show the same output every time. On the other hand, Iterator is a mutable data structure. Once you used a value, that value will be forever gone. Print the contents of i2 twice, and it will be empty the second time around:

    scala> s2 foreach println
    1
    5
    25
    
    scala> s2 foreach println
    1
    5
    25
    
    scala> i2 foreach println
    1
    5
    25
    
    scala> i2 foreach println
    
    scala> 
    

    Stream, on the other hand, is a lazy collection. Once a value has been computed, it will stay computed, instead of being discarded or recomputed every time. See below one example of that behavior in action:

    scala>     val s2 = s1.takeWhile(_ < 100)    // s2 is finite
    s2: scala.collection.immutable.Stream[Int] = Stream(1, ?)
    
    scala> println(s2)
    Stream(1, ?)
    
    scala> s2 foreach println
    1
    5
    25
    
    scala> println(s2)
    Stream(1, 5, 25)
    

    So Stream can actually fill up the memory if one is not careful, whereas Iterator occupies constant space. On the other hand, one can be surprised by Iterator, because of its side effects.

    (1) As a matter of fact, Iterator is not a collection at all, even though it shares a lot of the methods provided by collections. On the other hand, from the problem description you gave, you are not really interested in having a collection of numbers, just in iterating through them.

    (2) Actually, though takeWhile will create a new Iterator on Scala 2.8.0, this new iterator will still be linked to the old one, and changes in one have side effects on the other. This is subject to discussion, and they might end up being truly independent in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had the same question as was asked in this thread , i.e. I
I didn't see any similar questions asked on this topic, and I had to
I had this answer on another post I asked: I believe the VS designer
I had telephone interview question yesterday. The interviewer asked me if I had faced
Had a coworker ask me this, and in my brain befuddled state I didn't
I had a discussion with some colleagues mentioning that there are not too many
Had an interesting discussion with some colleagues about the best scheduling strategies for realtime
I had used Server Explorer and related tools for graphical database development with Microsoft
I had been happily coding along on a decent sized solution (just over 13k
I had an idea, if I add a python .py file to my C#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.