Beginners Task #2

Online C++ programming contests.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard, raimo

Postby Zen » Wed Jun 02, 2004 1:54 pm

- 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
User avatar
Zen
 
Posts: 1088
Joined: Wed Sep 24, 2003 1:41 am
Location: Norway

Throwing in the towel

Postby RaH » Wed Jun 02, 2004 3:04 pm

Alvaro your explanaition made it clear, but I'm still not sure of the syntax. So on this task I'm throwing in the towel.
"Codito ergo sum"
RaH
 
Posts: 258
Joined: Mon May 24, 2004 11:59 am
Location: Va Beach, Va.

Postby MXP » Wed Jun 02, 2004 5:00 pm

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);
   }
}
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Postby Alvaro » Wed Jun 02, 2004 5:04 pm

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

I don't see what you are trying to say here.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby Zen » Wed Jun 02, 2004 5:12 pm

I was just trying to give RaH somthing to work on.
foreach (FactorItem temp in factors)

Is this valid c# syntax? :shock:
Code: Select all
Console.Write("({0}^{1})", temp.Factor, temp.Power)

temp.Factor and temp.Power replace {0} and {1}, right?
User avatar
Zen
 
Posts: 1088
Joined: Wed Sep 24, 2003 1:41 am
Location: Norway

Postby MXP » Wed Jun 02, 2004 5:23 pm

Yes and yes.
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Postby Word2 » Wed Jun 02, 2004 6:16 pm

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);
   }
}

Somthing tells me your not a beginner.
14 and *trying* to code.

Image

Image
Word2
 
Posts: 128
Joined: Wed May 26, 2004 8:43 pm
Location: MI, USA

Ok sue me

Postby RaH » Thu Jun 03, 2004 10:02 am

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
//         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;
}

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. :!:
"Codito ergo sum"
RaH
 
Posts: 258
Joined: Mon May 24, 2004 11:59 am
Location: Va Beach, Va.

Re: Ok sue me

Postby Guest » Thu Jun 03, 2004 10:23 am

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. :!:


Part of the problems is that this :
Code: Select all
      }else if((UserPrime % 2) != 0)
      {
         answer = UserPrime / (n + 1);
         if(answer > 1)  // <-- will be true
         {
            answer = answer / (n + 1);
         }

the if will always change the value as long as answer is greater than zero.
Guest
 

Postby Guest » Thu Jun 03, 2004 10:26 am

If this helps I used this web site to check my output to see if they were correct.
http://newmanservices.com/primefact/
Guest
 

Re: Ok sue me

Postby Guest » Thu Jun 03, 2004 11:31 am

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)
      {

You also probably want to return answer before you loop again the loop you have will not return answer until the while loop fails
Guest
 

Update

Postby RaH » Thu Jun 03, 2004 12:09 pm

Ok I have gotten around the math issue.
Now it is a matter of outputting the correct text.
Here is my code.
Code: Select all
//     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;
}

Now the program executes correctly. Any tips on how to have the output print out? Without oveer writing my values?

current output:
*NOTE: Comments do not appear in the actual output, but reflect what is happening inside the loops*

Enter a number to be factored
to lowest prime numbers: 36
18 // 36 / 2
9 // not divisible by 2 so divide by 3 9 / 3
// which are both correct
3 //
// so what i have is n = 2 and again n = 2 and n = 3 and again n = 3

but here is my final output

Prime factors of 36 are
3 * 1
I would like some help on how to have it properly display 2, 2, 3, 3

Thanks guys for all the help.
"Codito ergo sum"
RaH
 
Posts: 258
Joined: Mon May 24, 2004 11:59 am
Location: Va Beach, Va.

Postby Guest » Thu Jun 03, 2004 1:35 pm

What you could do is change:
Code: Select all
      if((UserPrime % n) == 0)
      {
      answer = UserPrime / n;
         cout << n << " * ";
      //answer = answer / n;
         UserPrime = answer;
       
      }

just change the 2 to n
and change:
Code: Select all
      if((UserPrime % n) != 0)
      {
         n = n + 1;
         
         //answer = UserPrime / n;
            //answer = answer / n;
           // cout << n << endl;
         //UserPrime = answer;
      }

change the 2 to n here to
I aslo would change this:
Code: Select all
   //FindPrime(UserPrime);
   if(UserPrime != 0)
   {FindPrime(UserPrime);
      //cout << "Prime factors of " << UserPrime << " are\n" << n << " * " << answer << endl;
   }

also move the "cout << "Prime factors of " << UserPrime << " are\n" << n << " * " << answer << endl;" to the top of your funtion

Code: Select all
int FindPrime(int UserPrime)
{cout << "Prime factors of " << UserPrime << " are\n";
   while(UserPrime > 1)
   {

These are not the best solutions but they will work
Guest
 

Guest

Postby RaH » Thu Jun 03, 2004 2:29 pm

Hey thanks Guest. That helped my output, but I found an error in my code. It works great until you enter an odd integer. Weird. A little more time and I''l be ready to post it.
"Codito ergo sum"
RaH
 
Posts: 258
Joined: Mon May 24, 2004 11:59 am
Location: Va Beach, Va.

Postby Guest » Thu Jun 03, 2004 3:35 pm

You are welcome.

If the number is a prime already you will not get any out put. If you are unsure of the out put try the link on the above post.

On a side note, the way the function is it should really be void since you do not use the return value if you change to void you will need to remove the "return answer;" from your function.
Code: Select all
int FindPrime(int UserPrime);

void FindPrime(int UserPrime);// to void
Guest
 

PreviousNext

Return to Contests

Who is online

Users browsing this forum: No registered users and 0 guests