I'm doing a challenge on TalentBuddy that requires you to find the powers of multiple array pairs. Since I got the solution right, but not fast enough, I decided I'd ask for help here.
def fast_power(a, b)
pairs = a.zip(b)
pairs.each {|p| puts (p[0] ** p[1]) % 4211}
end
a1 = [long array with 5000 elements]
b1 = [Long array with 5000 elements]
Each element is a number between 0 and 50000.
I have to calculate the value of every element in the first array to the power of every corresponding element in the second array and then modulo that by 4211.
I ran benchmark tests on the data set and here is what I got
14.907000 0.734000 15.641000 (16.818161)
It was supposed to finish in under 2 seconds, so perhaps my algorithm is the issue.
Problem
Searching for a string in a large data store by simply comparing characters would take ages or a huge amount of computing power. We need a way to do it fast!
The first step to building a fast search algorithm is to encode the data in a format that makes search efficient.
A critical operation in the encoding process which we’ll get familiar with a bit later is the exponentiation of integer numbers.
Given a list of pairs of integers (a,b)
Your task is to
Write a function that prints to the standard output (stdout) for each pair the result of (ab) modulo 4211 (one per line)
Note that your function will receive the following arguments:
a - which is an array of integers giving the first element (a) of each pair
b - which is an array of integers giving the second element (b) of each pair
The ith pair is defined by a[i] and b[i].
Data constraints
The length of a, b arrays will not exceed 10000 elements of a, b arrays are integer numbers in the [0, 50000]
Efficiency constraints
Your function is expected to print the requested result and return in less than 2 seconds make sure you don't use any methods that do significant work for you (e.g. pow() in Python)