0
\$\begingroup\$

The task is to assign the n numbers given as input by the user in an array . For Ex : If user gives 10 as input then the generated array should be like this ,

arr[0] = 0
arr[1] = 1
arr[2] = 2
arr[3] = 3
.
.
.
arr[10] = 10 

The code below works fine but i am using a loop to assign this numbers to an array which can prove a lot of run-time during execution if the user gives an input like 10^6 .

puts "enter the number of times you want to test"
times = gets.chomp.to_i
1.upto times do |i|
 puts "enter the total number elements in the array ."
 no = gets.chomp.to_i
 puts "total number of elements are #{no + 1}"
 arr = []
 sum = 0
 0.upto no do |i|
 arr[i] = i
 end
 arr.each_index { |index| print "#{index} " }
 sum = arr.reduce(:+)
 puts "#{sum}" 
end

So , how should i optimize this code for better performance ?

Mathieu Guindon
75.5k18 gold badges194 silver badges467 bronze badges
asked Nov 17, 2015 at 17:29
\$\endgroup\$
4
  • 2
    \$\begingroup\$ Your title is an extremely generic one that applies to most questions on this site — see How to Ask. Please explain what you mean by "this code". Are you asking about how to sum an array, or are you asking about how rewrite the entire program (which might not involve an array at all)? \$\endgroup\$ Commented Nov 17, 2015 at 17:34
  • \$\begingroup\$ The problem is, I'm not sure what you're asking. It's an unclear question. \$\endgroup\$ Commented Nov 17, 2015 at 17:36
  • 2
    \$\begingroup\$ The title of a code review should state the purpose of the code. You are free to request specific concerns in the code review itself. \$\endgroup\$ Commented Nov 17, 2015 at 17:37
  • \$\begingroup\$ My problem is that the first loop in my program takes way too much running time if a large number is given as input , so what should i do so that it takes less runtime ? \$\endgroup\$ Commented Nov 17, 2015 at 17:50

1 Answer 1

2
\$\begingroup\$

There's no need to #chomp before calling #to_i.

The outer i is never used. In fact, it's confusing, because you introduce an inner i later. If all you want to do is repeat some code, use #times instead of #upto.

no looks like "yes"/"no". By convention, n is a good name for a number, if you don't have any better name for it.

The code would be more efficient if you didn't use any array at all. Just use the series formula:

$$\sum_{i=0}^n i = \dfrac{n (n - 1)}{2}$$

puts "enter the number of times you want to test"
gets.to_i.times do
 puts "enter the total number elements in the array ."
 n = gets.to_i
 puts "total number of elements are #{n + 1}"
 (0..n).each { |i| print "#{i} " }
 puts n * (n - 1) / 2
end
Nakilon
1,3387 silver badges17 bronze badges
answered Nov 17, 2015 at 18:04
\$\endgroup\$
6
  • 1
    \$\begingroup\$ You could also do print (0..n).to_a.join(" ") + " ". \$\endgroup\$ Commented Nov 17, 2015 at 18:58
  • \$\begingroup\$ @200_success : where are you assigning the 0 to n numbers to an array which is what i want ? . you are just printing it out . please let me know if there is a better way of assigning the elements in the array for my particular problem. \$\endgroup\$ Commented Nov 18, 2015 at 4:57
  • \$\begingroup\$ There is no array because you don't need one to accomplish the task, and it's faster without an array. \$\endgroup\$ Commented Nov 18, 2015 at 5:00
  • \$\begingroup\$ Ok . Let's assume we want to assign this 0 to n numbers to an array without using any loop so what should be the approach ? \$\endgroup\$ Commented Nov 18, 2015 at 5:02
  • 1
    \$\begingroup\$ Why? Please edit the question to clarify. \$\endgroup\$ Commented Nov 18, 2015 at 5:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.