根据给定的数字查找其因数的高效C++代码
//Algorithm derived from the method described here: //http://mathschallenge.net/index.php?section=faq&ref=number/number_of_divisors int mathTools::numFactors(unsigned long long number){ if(number < 2){ return 1; } //Answer starts at one because all numbers can be divided by 1 int answer = 1; int counter = 0; //Adds up the number of times divisible by 2 while(number % 2 == 0){ number = number / 2; counter++; } answer = answer * (counter + 1); counter = 0; //Gets all other multiples of the number except possibly the last for(unsigned long long i = 3; i < ceil(sqrt(number)) + 1; i++){ while(number % i == 0){ number = number / i; counter++; } answer = answer * (counter + 1); counter = 0; } //If the last number is not a square this will account for it. if(ceil(sqrt(number) != floor(sqrt(number)))){ counter++; } answer = answer * (counter + 1); return answer; }