ROBOTC.net forums
http://www.robotc.net/forums/

Prime Factorization of Positive Integers
http://www.robotc.net/forums/viewtopic.php?f=15&t=5479
Page 1 of 1

Author:  Coder A [ Mon Feb 11, 2013 5:41 pm ]
Post subject:  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

Author:  ernest314 [ Sat Jun 01, 2013 11:30 pm ]
Post subject:  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 :)

Author:  Coder A [ Thu Aug 22, 2013 6:15 pm ]
Post subject:  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!

Author:  Ernest3.14 [ Fri Aug 23, 2013 12:48 pm ]
Post subject:  Re: Prime Factorization of Positive Integers

For starters, there's this: http://projecteuler.net/problems

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/