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

  • Home
  • SEARCH
  • 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 7914919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:16:55+00:00 2026-06-03T14:16:55+00:00

New to StackOverflow here. I’m working on the first Euler problem and have run

  • 0

New to StackOverflow here. I’m working on the first Euler problem and have run into an issue where I can get the statement to iterate through the array. It seems like it has something to do with the way I have the while loop setup but I can’t figure it out.

Here’s my code:

#euler problem 1

numbers = [3,5]
sum = 0
i=1
total=0

numbers.each do |number|
  while i * number < 10
    adder = i * number
    total += adder
    i += 1
    puts total
  end
end

puts total

The output is 3
9
18
18

Any idea why it isn’t processing the 5 in the array numbers?

  • 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-06-03T14:16:57+00:00Added an answer on June 3, 2026 at 2:16 pm

    Your problem is that i is declared outside the block so when number is five, i is already four and the while loop’s condition fails immediately because 20 < 10 is false. Try it like this:

    numbers = [3,5]
    sum = 0
    total=0
    
    numbers.each do |number|
      i = 1
      while i * number < 10
        #...
      end
    end
    
    puts total
    

    If you put a little puts in your code you’ll see what’s going on:

    i = 1
    numbers.each do |number|
      puts "#{number}\ti = #{i}"
      while i * number < 10
        puts "\ti = #{i}"
        adder = i * number
        total += adder
        i += 1
      end
    end
    

    That will give you this output:

    3   i = 1
        i = 1
        i = 2
        i = 3
    5   i = 4
    

    and you’ll see the problem with i.

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

Sidebar

Related Questions

I'm new here to stackoverflow, so bear with me. I have a book that
I'm new to Ruby, Regex and Stackoverflow. xD Here's my problem: I want to
I have found here on stackoverflow a solution to extend django-registration with new fields
Ruby and StackOverflow newb here working my way through Ruby and ran into my
This is my first question here on StackOverflow, and quite frankly I'm fairly new
Brand new poster here on StackOverflow. I've been reading and learning here for some
I am new here and hope my first question meets stack overflow's requirements. I
I'm new to programming and stackoverflow and have decided to start by learning objective
Frequent visitor but first post here on StackOverflow, I'm hoping that you guys might
I was looking for a similar issue here at Stackoverflow javascript multiple date display

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.