View unanswered posts | View active topics It is currently Sat Oct 25, 2014 2:31 pm






Reply to topic  [ 4 posts ] 
Prime Factorization of Positive Integers 
Author Message
Moderator
Moderator

Joined: Thu Jan 03, 2013 5:10 pm
Posts: 198
Location: The plateau north of the Ohio River Valley, also known as Cave Country.
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


Mon Feb 11, 2013 5:41 pm
Profile
Rookie

Joined: Sun Nov 11, 2012 6:21 pm
Posts: 5
Post Re: Prime Factorization of Positive Integers
Recursive solutions are cool, but not the most efficient (if you deal with large numbers, you might run out of resources). Have you taken a look at Eratosthenes' sieve? It's pretty handy :)


Sat Jun 01, 2013 11:30 pm
Profile
Moderator
Moderator

Joined: Thu Jan 03, 2013 5:10 pm
Posts: 198
Location: The plateau north of the Ohio River Valley, also known as Cave Country.
Post Re: Prime Factorization of Positive Integers
Actually, I am using an iterative approach. And Eratosthenes' sieve is neat, but not good for determining if just a single number is false, because you must determine all the primes lesser than the input number as well, which leads to memory issues, much in the same way a recursive algorithm would. But thanks for the comment!

_________________
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


Thu Aug 22, 2013 6:15 pm
Profile
Professor
User avatar

Joined: Sat May 18, 2013 1:24 pm
Posts: 272
Location: Olympia, WA
Post Re: Prime Factorization of Positive Integers
For starters, there's this: http://projecteuler.net/problems

_________________
FTC Team 6424, the 'Oly Cow - Chief programmer.
FRC Team 4450, Olympia Robotics Federation (ORF).

and also quadrotors. Quadrotors!


Fri Aug 23, 2013 12:48 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

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.