I am using unsigned long long integer format in order to calculate big factorials. However my code fails at some point can you have a look at it? Actually it is part of a la
A simple algorithm that i wrote. but it's in Java.. You can calculate factorial of 1000 in about 15 minutes.
This algorithm works with the basic formula that we learnt at primary school.
/* FOR BEST RESULT DON'T CHANGE THE CODE UNTIL YOU KNOW WHAT YOU'RE DOING */
public String factorial(int number){
if(number == 0) return "1";
String result = "1";
for(int i = 0; i < number; i++){
result = *longNumberMultiplyingAlgorithm*(result, "" + (i + 1));
}
return result;
}
public String longNumberMultiplyingAlgorithm(String number1, String number2){
int maxLength = Math.max(number1.length(), number2.length());
int a = 0;
String[] numbers = new String[maxLength];
if(number2.length() > number1.length()){
String t = number1;
number1 = number2;
number2 = t;
}
for(int i = 0; i < number1.length(); i++){
numbers[i] = "";
a = 0;
for(int j = 0; j < number2.length(); j++){
int result = Integer.parseInt(String.valueOf(number1.charAt(number1.length() - i - 1))) * Integer.parseInt(String.valueOf(number2.charAt(number2.length() - j - 1)));
if(result + a < 10){
numbers[i] = (result + a) + "" + numbers[i];
a = 0;
}else{
result += a;
a = (int)((result + 0.0) / 10);
result -= a * 10;
numbers[i] = result + "" + numbers[i];
}
}
if(a != 0){
numbers[i] = a + "" + numbers[i];
}
for(int k = 0; k < i; k++){
numbers[i] += "0";
}
}
return longNumberAdditionAlgorithm(numbers);
}
private String longNumberAdditionAlgorithm(String[] numbers) {
String final_number = "0";
for(int l = 0; l < numbers.length; l++){
int maxLength = Math.max(final_number.length(), numbers[l].length());
String number = "";
int[] n = new int[maxLength];
int a = 0;
for(int i = 0; i < n.length; i++){
int result = 0;
if(i >= final_number.length()){
result = Integer.parseInt(String.valueOf(numbers[l].charAt(numbers[l].length() - i - 1)));
}else
if(i >= numbers[l].length()){
result = Integer.parseInt(String.valueOf(final_number.charAt(final_number.length() - i - 1)));
}else{
result = Integer.parseInt(String.valueOf(final_number.charAt(final_number.length() - i - 1))) + Integer.parseInt(String.valueOf(numbers[l].charAt(numbers[l].length() - i - 1)));
}
if(result + a < 10){
number = (result + a) + "" + number;
a = 0;
}else{
result -= 10;
number = (result + a) + "" + number;
a = 1;
}
}
if(a == 1){
number = a + "" + number;
}
final_number = number;
}
return final_number;
}