- The smallest divisor of a number is always prime
Try first and write a program that find all possible combinations with two factors (nonprime too)
etc:
24 = 1*24
24 = 2*12
...
24 = 8*3
Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard, raimo
//Factorizer - factors numeric input
//Colin Jeanne, June 2004
//http://www.invadersrealm.com
using System;
using System.Collections;
struct FactorItem {
public int Factor; //This is the factor of the number
public int Power; //This is how many times it goes into our number
}
class Factorize {
//Since this program is so simple we only need one function
public static void Main() {
Console.Write("Enter a number to factor: ");
int number = Int16.Parse(Console.ReadLine());
ArrayList factors = new ArrayList();
FactorItem fi;
//2 is the only even prime, we'll treat it as a special case
if (number % 2 == 0) {
//number is divisible by 2 and so we build the structure
fi.Factor = 2;
fi.Power = 0;
while (number % fi.Factor == 0) {
number /= fi.Factor;
++fi.Power;
}
factors.Add(fi);
}
//Now we work with every odd number
for (fi.Factor = 3; fi.Factor <= number; fi.Factor += 2) {
if (number % fi.Factor == 0) {
//We have found a factor and so we divide number by it until we
//cant divide it anymore
fi.Power = 0;
while (number % fi.Factor == 0) {
number /= fi.Factor;
++fi.Power;
}
factors.Add(fi);
}
}
//Write our each factor
foreach (FactorItem temp in factors)
Console.Write("({0}^{1})", temp.Factor, temp.Power);
}
}
Zen wrote:- The smallest divisor of a number is always prime
Try first and write a program that find all possible combinations with two factors (nonprime too)
etc:
24 = 1*24
24 = 2*12
...
24 = 8*3
foreach (FactorItem temp in factors)
Console.Write("({0}^{1})", temp.Factor, temp.Power)Colin Jeanne wrote:No language was specified so I did this too... in C#
- Code: Select all
//Factorizer - factors numeric input
//Colin Jeanne, June 2004
//http://www.invadersrealm.com
using System;
using System.Collections;
struct FactorItem {
public int Factor; //This is the factor of the number
public int Power; //This is how many times it goes into our number
}
class Factorize {
//Since this program is so simple we only need one function
public static void Main() {
Console.Write("Enter a number to factor: ");
int number = Int16.Parse(Console.ReadLine());
ArrayList factors = new ArrayList();
FactorItem fi;
//2 is the only even prime, we'll treat it as a special case
if (number % 2 == 0) {
//number is divisible by 2 and so we build the structure
fi.Factor = 2;
fi.Power = 0;
while (number % fi.Factor == 0) {
number /= fi.Factor;
++fi.Power;
}
factors.Add(fi);
}
//Now we work with every odd number
for (fi.Factor = 3; fi.Factor <= number; fi.Factor += 2) {
if (number % fi.Factor == 0) {
//We have found a factor and so we divide number by it until we
//cant divide it anymore
fi.Power = 0;
while (number % fi.Factor == 0) {
number /= fi.Factor;
++fi.Power;
}
factors.Add(fi);
}
}
//Write our each factor
foreach (FactorItem temp in factors)
Console.Write("({0}^{1})", temp.Factor, temp.Power);
}
}
// 3 June 2004 //
// pfactor.exe //
///////////////////////////////
#include <iostream>
int answer;
int n = 2;
int FindPrime(int UserPrime);
int main()
{
int UserPrime;
Start:
cout << endl;
cout << "Enter a number to be factored\nto lowest prime numbers: ";
cin >> UserPrime;
// check for a one or zero entered
if(UserPrime <= 1)
{
cout << "Error!\tYou entered " << UserPrime << "\nPlease try again.\n";
goto Start;
}
FindPrime(UserPrime);
if(answer != 0)
{
cout << "Prime factors of " << UserPrime << " are\n" << n << " * " << answer << endl;
}
return 0;
}
int FindPrime(int UserPrime)
{
while(UserPrime > 1)
{
if((UserPrime % 2) == 0)
{
answer = UserPrime / n;
if(answer > 1)
{
answer = answer / n;
}
}else if((UserPrime % 2) != 0)
{
answer = UserPrime / (n + 1);
if(answer > 1)
{
answer = answer / (n + 1);
}
}
UserPrime = answer;
}
return answer;
}
RaH wrote:So I haven't given up. I have gotten some code down, but I am stuck on the math of the prime function. I have tried to apply the examples that were given but I am running into a wall. Here is what I have. Please feel free to give me tips pointers etc.
- Code: Select all
}else if((UserPrime % 2) != 0)
{
answer = UserPrime / (n + 1);
if(answer > 1)
{
answer = answer / (n + 1);
}
}
UserPrime = answer;
}
return answer;
}
I need to know where I went wrong with this. Or how I can improve on it? Or if I should chuck it and start over.
}else if((UserPrime % 2) != 0)
{
answer = UserPrime / (n + 1);
if(answer > 1) // <-- will be true
{
answer = answer / (n + 1);
}RaH wrote:So I haven't given up. I have gotten some code down, but I am stuck on the math of the prime function. I have tried to apply the examples that were given but I am running into a wall. Here is what I have. Please feel free to give me tips pointers etc.
- Code: Select all
int FindPrime(int UserPrime)
{
while(UserPrime > 1)
{
if((UserPrime % 2) == 0)
{
// 3 June 2004 //
// pfactor.exe //
///////////////////////////////
#include <iostream>
int answer;
int n = 2;
int FindPrime(int UserPrime);
int main()
{
int UserPrime;
Start:
cout << endl;
cout << "Enter a number to be factored\nto lowest prime numbers: ";
cin >> UserPrime;
// check for a one or zero entered
if(UserPrime <= 1)
{
cout << "Error!\tYou entered " << UserPrime << "\nPlease try again.\n";
goto Start;
}
FindPrime(UserPrime);
if(answer != 0)
{
cout << "Prime factors of " << UserPrime << " are\n" << n << " * " << answer << endl;
}
return 0;
}
int FindPrime(int UserPrime)
{
while(UserPrime > 1)
{
if((UserPrime % 2) == 0)
{
answer = UserPrime / n;
answer = answer / n;
UserPrime = answer;
}
if((UserPrime % 2) != 0)
{
n = n + 1;
answer = UserPrime / n;
answer = answer / n;
cout << answer << endl;
UserPrime = answer;
}
UserPrime = answer;
}
return answer;
}
if((UserPrime % n) == 0)
{
answer = UserPrime / n;
cout << n << " * ";
//answer = answer / n;
UserPrime = answer;
} if((UserPrime % n) != 0)
{
n = n + 1;
//answer = UserPrime / n;
//answer = answer / n;
// cout << n << endl;
//UserPrime = answer;
} //FindPrime(UserPrime);
if(UserPrime != 0)
{FindPrime(UserPrime);
//cout << "Prime factors of " << UserPrime << " are\n" << n << " * " << answer << endl;
}int FindPrime(int UserPrime)
{cout << "Prime factors of " << UserPrime << " are\n";
while(UserPrime > 1)
{int FindPrime(int UserPrime);
void FindPrime(int UserPrime);// to voidUsers browsing this forum: No registered users and 0 guests