Beginners Task #2

Online C++ programming contests.

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

Beginners Task #2

Postby tomcant » Sat May 29, 2004 4:04 am

Beginners Task #2: Prime Factors

I hope most of you know what a prime factor is, but for those of you who dont: A prime factor is a factor of a number that is also a prime number. ie. 7 is a prime factor of 56 and 13 is a prime factor of 169.

Example:
Image

So it can be said that 2, 2, 3, and 3 are the prime factors of 36. ie. When you do 2 x 2 x 3 x 3, you get 36.

Input and Output

The user input for this program should look like this:
Enter number: 36


The output of the program should look something like:
Prime factors: 2 x 2 x 3 x 3



Don't really know about a deadline.... we'll discuss that later

Happy codin'...
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby RecursiveS » Sat May 29, 2004 7:32 am

Stickied for a while, as last time.
User avatar
RecursiveS
Site Admin
 
Posts: 1236
Joined: Thu Sep 18, 2003 8:33 am
Location: Dorset, UK

Postby Guest » Sat May 29, 2004 8:59 am

Okay I am in. It will be a few days before I can post anything, busy weekend.
Guest
 

Postby schloob » Sat May 29, 2004 9:42 am

Code: Select all
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int prime(int);

int vectorisprime(vector<int> o){

   if (o.size()==0)
      return 0;
   
   for (int i=0;i<o.size();i++){

      if (prime(o[i]))
         return 0;

   }

   return 1;

}

int prime(int n){ // (!prime(x))=prime

   int i;

   for (i=2;i<=sqrt(n);i++){

      if (n%i==0)
         return i;

   }

   return 0;

}

void prime_factors(int n, vector<int> &o){

   o.clear();

   o.push_back(n);

   int i,c;

   while (!vectorisprime(o)){

      for (i=0;i<o.size();i++){

         if ((c=prime(o[i]))){

            o.push_back(c);

            o.push_back(o[i] / (double)c);

            o.erase(o.begin()+i);

            break;
                        
         }

      }

   }

}

int main(){

   vector<int> o;

   int n;

   cin>>n;

   if (!cin) return 1;

   prime_factors(n,o);

   cout<<"factors: "<<endl;

   for (int i=0;i<o.size();i++)
      cout<<o[i]<<" x "<<flush;

   cout<<"\b\b\b   "<<endl;
   
   return 0;

}


*Edit*

latest update

Code: Select all
void factor(int n){
for (int i=2;i<n/2.0;i++)
if (!(n%i)){cout<<i<<" x "<<flush;factor(n/i);return;}
cout<<n<<flush;
}
Last edited by schloob on Sun Jun 06, 2004 9:18 am, edited 1 time in total.
:]
User avatar
schloob
 
Posts: 1853
Joined: Mon Feb 16, 2004 10:29 am
Location: Seattle

Postby tomcant » Sat May 29, 2004 12:19 pm

Nice one, I hadn't thought of using vectors :wink:
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby Guest » Sun May 30, 2004 11:31 am

schloob I can not get yours to compile it gives me errors with the sqrt.
Code: Select all
for (i=2;i<=sqrt(n);i++)
Guest
 

Postby C++ » Sun May 30, 2004 12:29 pm

Don't people comment their code anymore :?:
C++
 
Posts: 687
Joined: Fri Nov 28, 2003 9:42 pm
Location: NYC, USA

Postby schloob » Sun May 30, 2004 1:03 pm

dev cpp version:

Code: Select all
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int prime(int);

int vectorisprime(vector<int> o){

   if (o.size()==0)
      return 0;
   
   for (int i=0;i<o.size();i++){

      if (prime(o[i]))
         return 0;

   }

   return 1;

}

int prime(int n){ // (!prime(x))=prime

   int i;

   for (i=2;i<=(int)sqrt((double)n);i++){

      if (n%i==0)
         return i;

   }

   return 0;

}

void prime_factors(int n, vector<int> &o){

   o.clear();

   o.push_back(n);

   int i,c;

   while (!vectorisprime(o)){

      for (i=0;i<o.size();i++){

         if ((c=prime(o[i]))){

            o.push_back(c);

            o.push_back((int)((double)o[i] / (double)c));

            o.erase(o.begin()+i);

            break;
                       
         }

      }

   }

}

int main(){

   vector<int> o;

   int n;

   cin>>n;

   if (!cin) return 1;

   prime_factors(n,o);

   cout<<"factors: "<<endl;

   for (int i=0;i<o.size();i++)
      cout<<o[i]<<" x "<<flush;

   cout<<"\b\b\b   "<<endl;
   
   system("pause");
   
   return 0;

}
:]
User avatar
schloob
 
Posts: 1853
Joined: Mon Feb 16, 2004 10:29 am
Location: Seattle

Postby schloob » Sun May 30, 2004 1:04 pm

C++ wrote:Don't people comment their code anymore :?:


commenting is for suckers :]
:]
User avatar
schloob
 
Posts: 1853
Joined: Mon Feb 16, 2004 10:29 am
Location: Seattle

Postby Guest » Sun May 30, 2004 1:50 pm

schloob wrote:dev cpp version:

Very nice. This version worked fine for me thank you schloob.
Guest
 

Postby C++ » Sun May 30, 2004 2:55 pm

schloob wrote:
C++ wrote:Don't people comment their code anymore :?:


commenting is for suckers :]


Not when 25% of your project's grade depends on it
C++
 
Posts: 687
Joined: Fri Nov 28, 2003 9:42 pm
Location: NYC, USA

Postby schloob » Sun May 30, 2004 3:03 pm

project? what project...
:]
User avatar
schloob
 
Posts: 1853
Joined: Mon Feb 16, 2004 10:29 am
Location: Seattle

Postby C++ » Sun May 30, 2004 4:04 pm

Class projects....

Commenting also helps you understand your programs later on. Write a 1000 line program, don't comment it at all, go back to it 3 months later and you won't be able to tell what your program's doing, it'll take a couple of hours to figure things out. But if you comment everything, you can easily read the comments and in minutes you'll know what the code is doing. The same applies if another person is reading your code.

You should comment every variable, stating what it is for
Comment every loop stating the conditions and what it does
Comment every function stating what it takes as parameters, what it does, and what it returns, if any.
Put a comment on top of your program stating the name of the program, your name, last modification date, starting date, purpose of the program, and anything else you feel is important.
C++
 
Posts: 687
Joined: Fri Nov 28, 2003 9:42 pm
Location: NYC, USA

Postby Guest » Sun May 30, 2004 4:17 pm

This is my version of the task. The program does have some limitations: if any of the primes are over 541 it will not work.
Code: Select all
#include <iostream>

using namespace std;

long primenext(long* prime, long next);

int main()
{
   for(;;)
   {
      long prime = 2;
      int answer;
      cout << endl;
      cout << "Enter a number for Prime Factors : " << endl;
      cin >> answer;
      long factor = 0;
      long next = 1;
      if (answer > 0)
      {
         do
         {
            if (answer % prime == 0)
            {
               factor = answer / prime;
               if (answer == prime)//the end if they are ==
                  answer = 0;
               else
                  answer = factor;
               cout << prime << ((0 == answer) ? "." : "x");
            }
            else                     
            {
               if (next == 100)
               {
                  cout << endl << "One of the Prime Factors is over 541" << endl;
                  break;
               }                  //next++ will decide the array number
               primenext(&prime , next++);//get next prime
            }
         }while(answer > 0);
      }
      else
      {
         cout << "That was out of range." << endl;
         return 0;
      }
   }
}

long primenext(long* prime, long next) //prime number generator
{
   long totalprimes[100] = {2,3};//keep prime to check for primes
   long count = 2;
   long test = 3;
   bool isprime = true;

   if(*prime == 2)
      *prime =3;
   else
   {
      do
      {
         test += 2;
         int i =0;
         do
         {
            isprime = test % totalprimes[i] > 0;//find next prime
         }while(++i < count && isprime);
         if (isprime)
            totalprimes[count++] = test;//store in the array
      }while(count < next && count < 100);

      *prime = test;//take new prime
      return *prime;
   }
}
Guest
 

Postby schloob » Sun May 30, 2004 4:53 pm

C++ wrote:Class projects....

Commenting also helps you understand your programs later on. Write a 1000 line program, don't comment it at all, go back to it 3 months later and you won't be able to tell what your program's doing, it'll take a couple of hours to figure things out. But if you comment everything, you can easily read the comments and in minutes you'll know what the code is doing. The same applies if another person is reading your code.

You should comment every variable, stating what it is for
Comment every loop stating the conditions and what it does
Comment every function stating what it takes as parameters, what it does, and what it returns, if any.
Put a comment on top of your program stating the name of the program, your name, last modification date, starting date, purpose of the program, and anything else you feel is important.


lol i have like a 2000-3000 line program i havent worked on for ~5-6 months, i go back to it and its still very easy to understand :-]

although my word count program is hard as hell to understand (for me, slow mind :-p)

if you want to see it, this is it

http://poop.mine.nu/wordcount.txt

anyways, comments just arent my style :-\

*edit*

also, i dont take a programming class so i just dont need to comment :-\
:]
User avatar
schloob
 
Posts: 1853
Joined: Mon Feb 16, 2004 10:29 am
Location: Seattle

Next

Return to Contests

Who is online

Users browsing this forum: No registered users and 2 guests