java - Calculate the power with multithreads -
i'm working on calculator calculates number power of number. happens code:
public biginteger generate(long power, long base){ biginteger result = biginteger.valueof(base), = biginteger.valueof(base); int j=0; while(j!=power-1){ result = result.multiply(a); j++; } return result; }
i know if there mathematical way split type of calculation multiple threads, program calculate things 987654321 ^ 987654321 faster. cpu have 6 cores if there way use them @ once stuff great.
the first thing comes mind factorization, i.e., write base number product
base = a_1 * ... * a_n
and compute a_i^b
on separate threads, , multiply results. need computational effort finding factorization, first check if numbers ´big enough´ bother multiple cores, , if are, start standard checks if factorization possible. or divide first 100 prime numbers, should not take long.
especially, if number factorizes several times same factor, have compute power once, i.e.,
(24)^5 = (3*2*2*2)^5 = 3^5 * 2^5 * 2^5 * 2^5
and compute r_1 = 3^5
, r_2 = 2^5
, multiply result r = r_1 * r_2 * r_2 * r_2
.
you can factorize exponents, algorithm tad more complicated.
Comments
Post a Comment