I have created a program which takes a value x
and uses this value to create a power of ten value like so 10 ** x
.
This has then been used as the maximum number of a range 1..(10 ** x)
, which has been expanded using the splat *
method to create an array of numbers, and then assigned to the variable num_array
.
Using the values in this array, I want to find the total number of increasing and decreasing integers within this range. These are integers containing digits that either ascend or descend with each proceeding digit.
For example, if
x = 3
then
num_array = [*1..(10**3)] #=> num_array = [1, 2, 3, 4, .. 998, 999, 1000]
Then numbers like 234
which is ascending and 987
which is descending should be counted. Numbers like 555
fall into both categories so also should be counted but numbers like 482
should not because it doesn't follow either rule.
The each
method is used to evaluate which integers follow this rule in the range.
My problem is as the powers of ten increase with each method call at the end of the program, it gets slower and slower to return the end result until it calls total_nums(6)
which then takes too long.
- Is there a general rule of thumb to help process really large arrays like this so it's much faster for your program to return the end result?
- What would you say are your preferred methods for such an approach like this?
- And would something like Memoization work here?
def total_nums(x)
return 0 if x == nil
return 1 if x == 0
num = 10 ** x
num_array = [*1..num]
total = 0
num_array.each do |i|
if i == i.to_s.split("").sort.join.to_i || i == i.to_s.split("").sort.reverse.join.to_i
total +=1
end
end
return total
end
total_nums(0) #=> 1
total_nums(1) #=> 10
total_nums(2) #=> 100
total_nums(3) #=> 475
total_nums(4) #=> 1675
total_nums(5) #=> 4954
total_nums(6) #=> 12952
100
or3100
even though their digits don't "ascend or descend with each proceeding digit". Is that correct? \$\endgroup\$555
it is considered to belong to both the ascending and descending category even though the digit is repeated. As long as the proceeding digits don't ascend then descend (or vice versa) through the integer than the case is considered true for that integer. \$\endgroup\$