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 ?
-
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\$200_success– 200_success2015年11月17日 17:34:31 +00:00Commented Nov 17, 2015 at 17:34
-
\$\begingroup\$ The problem is, I'm not sure what you're asking. It's an unclear question. \$\endgroup\$200_success– 200_success2015年11月17日 17:36:13 +00:00Commented 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\$Greg Burghardt– Greg Burghardt2015年11月17日 17:37:33 +00:00Commented 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\$huzefa biyawarwala– huzefa biyawarwala2015年11月17日 17:50:24 +00:00Commented Nov 17, 2015 at 17:50
1 Answer 1
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
-
1\$\begingroup\$ You could also do
print (0..n).to_a.join(" ") + " "
. \$\endgroup\$Flambino– Flambino2015年11月17日 18:58:03 +00:00Commented Nov 17, 2015 at 18:58 -
\$\begingroup\$ @200_success : where are you assigning the
0
ton
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\$huzefa biyawarwala– huzefa biyawarwala2015年11月18日 04:57:28 +00:00Commented 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\$200_success– 200_success2015年11月18日 05:00:15 +00:00Commented Nov 18, 2015 at 5:00
-
\$\begingroup\$ Ok . Let's assume we want to assign this
0
ton
numbers to an array without using any loop so what should be the approach ? \$\endgroup\$huzefa biyawarwala– huzefa biyawarwala2015年11月18日 05:02:37 +00:00Commented Nov 18, 2015 at 5:02 -
1\$\begingroup\$ Why? Please edit the question to clarify. \$\endgroup\$200_success– 200_success2015年11月18日 05:03:09 +00:00Commented Nov 18, 2015 at 5:03