I was wondering if someone could help me fix two of my algorithms. I am trying to factorize semi-prime numbers by using a brute force method and a square root method.
The brute force method should start by multiplying all odd numbers between 1 and the semi-prime number. Meaning that 1 should multiply 3 then 5 then 7, etc. Then three should multiply 5 then 7 then 9 and so forth. The first number would never multiply a number equal to or less than itself.
Here is what I have:
1 2 3 4 5 6 7 8 9 10 11
bruteForce(int x){
f1 = 1;
f2 = f1 + 2;
while (f1 < x){
for (f2 = x; f2 <= x; f2-2){
f1 * f2;
}
if (x%f1 == f2 || x%f2 == f1)
printFactors(f1,f2);
} f1 = f1 + 2;
};
For my other algorithm, I want it to get the square root of the semi-prime and then start multiplying odd numbers around it. I made it so that the square root would have to be set to the next even number (in case it was a factor). If the product of the two odd numbers (a * b) is greater than the original semi-prime, I would multiply b by the next smaller number than a. Conversely, if the product of a and b is less than the original semi-prime, it will multiply a by the next odd number larger than b. This will repeat until the two correct factors of the original semi-prime are found.
Algorithm:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
SquareRootFactorize(int x){
int N = x;
sqrtN = sqrt(N);
if (isOdd(sqrtN) == true)
sqrtN = sqrtN + 1;
f1 = sqrtN - 1;
f2 = sqrtN + 1;
cout << sqrtN;
while (N != n){
n = multFacts(f1,f2);
if (N < (f1 * f2))
f1 = f1 + 2;
elseif (N > (f1 * f2))
f2 = f2 + 2;
elseif (N = n)
printFactors(f1,f2);
}
}