View unanswered posts | View active topics It is currently Sun May 19, 2013 1:31 pm






Reply to topic  [ 1 post ] 
Prime Factorization of Positive Integers 
Author Message
Expert

Joined: Thu Jan 03, 2013 5:10 pm
Posts: 138
Location: Southern Indiana
Post Prime Factorization of Positive Integers
This is my code for factorizing a number input to the function and some code put around it. The function simply returns the smallest factor greater than one of its input (which is prime for any input other than 1 or 0), and the code repeatedly calls this function to determine the entire factorization of the number. My only problem is I don't know what I should use the function for other than math. Feel free to use it and tell me how you did so we can learn together.

Code:
short factor(long factand)
{
   if(factand <= 1)
   {
      return factand;
   }
   else
   {
      short divisor;
      bool isfactor = false;
      for(divisor = 2;!isfactor;divisor++)
      {
         isfactor = (factand%divisor==0);
      }
      return divisor-1;
   }
}

//------------------------------------------------------------------------------------------------------------------------//

task main()
{
   string factorization;
   string nextfactor;
   short numtofactor = 30;// Change this number if you like. It's the number this code factorizes.
   while(factor(numtofactor)>1)
   {
      factorization = factorization + "*";
      StringFormat(nextfactor,"%d",factor(numtofactor));
      factorization = factorization + nextfactor;
      numtofactor /= factor(numtofactor);
   }
   StringDelete(factorization,0,1);
   nxtDisplayCenteredTextLine(3,"%s",factorization);
   while(nNxtButtonPressed == -1){}
}


I don't even know if factand is a word!




-Coder A

_________________
I'm not a robot! I'm british! ~ quote from an asparagus
I am not a robot! I am a unicorn! ~ quote from a robot
Play chess for free!


Mon Feb 11, 2013 5:41 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.