Jordan B.

asked • 09/16/17

Python coding question

My current code is shown below, and I need to get it so that the code will print all the factors of that number, one in a line. But right now I'm not getting an output from the code. 

n_str = input("Input an int: ")
num_int = int(n_str)
divider = 1
factor = num_int // divider

while divider >= 1 and divider <= num_int:
---if num_int % divider == 0:
------print(factor)

------divider = (divider + 1)

Andy C.

// MY BAD !!!!
 

class FactorFinder
{
public static final long MAX_LIMIT = 1000000000; //set it at 1 billion for now

public static void main(String args[])
{


double dblInputNum;
long N;

if (args.length<=0)
{
System.out.println(" java FactorFinder N \n where N is the number ");
return;
}


dblInputNum = Double.parseDouble(args[0]);

if (dblInputNum >= 0)
{
N = (long) Math.floor(dblInputNum);
}
else
{
N = (long) Math.ceil(dblInputNum);
}

if (N>MAX_LIMIT) { System.out.println(" too big "); return; }

if (N<0) { N = -N; }

if (N==0) { System.out.println("0"); return; }

if (N==1) { System.out.println("1"); return; }

if ( (N>1) && (N<4))
{
System.out.print( "1 " + N); return;
}

System.out.print("1 ");

for (int iLoop=2; iLoop < N/2 +1; iLoop++)
{

if ((N % iLoop)==0)
{
System.out.print( iLoop + " ");

}
}

System.out.println(N);
}


}
Report

09/17/17

2 Answers By Expert Tutors

By:

Andy C. answered • 09/16/17

Tutor
4.9 (27)

Math/Physics Tutor

Tim C.

tutor
Andy,, prime factorization is not being asked for. Only factorization.
Report

09/17/17

Andy C.

OK, I see you are converting the input to an integer.
I admit that if a decimal number is input, the program will crash because it is expecting an integer.
Also, no upper bounds were specified on the input, so a long integer will also cause problems.
So perhaps changing the types from int to long maybe needed. However, I see you are using integers
so maybe this is not the case. In the main routine where the input is read from the command line,
the statement can be changed to something similar to this:
if (args.length>0) 
{
   N = (int) (Math.floor(Double.parseDouble(args[0])));
}
 
Besides all the petty details  and "bells and whistles"
THE MAIN IDEA is to understand the recursion per the 
following "pseudo code"
 
      PrimeFactor (X) 
      {
            prime_factor = 1  //assume there are no real prime factors
 
            for iLoop = 2 to X/2+1   // only need to go half way; if there is not divisor by then, there aren't any
            {
                if X mod iLoop is zero  //  found a prime factor; store it and get out
                   prime_factor = iLoop
                   break
            }
 
            if (prime_factor >1)  // prime factor was found
            {
                  output prime_factor  
                  X = X / prime_factor;   // divides it out
                  PrimeFactor(X);            // repeats the entire process recursively
            }
            else
            {
                output X;  // no prime factors were found; the number is prime, outputs it
            }
 
      }
 
--------------------------------------------------------------------------
 
Think of it like this, with input 100.
 
Prime factor 100--> Is there a divisor of 100 between 2 and 50? Yes, 2... outputs it, 100/2=50.
Prime factor 50 ---> Is there a divsor of 50 between 2 and 25? Yes, 2.. outputs it, 50/2 = 25.
Prime factor 25 --> Is there a diivisor of 25 between 2 and 13? Yes, 5... outputs it, 25/5 = 5.
Prime factor 5 --> Is there a divisor of 5 between 2 and 3? No. outputs 5. Done
 
Try to use for loops whenever possible. For loops are bound to end and not lock up
due to an infinite loop because conditions are not met when they need to be (or vice versa).
that is, for loops are less prone to error and bugs. Notice is start the loop at 2.
dividing by 1 does not accomplish anything. 
 
Report

09/16/17

Andy C.

/****************************************
    version #2
 
1) removed the recursion and replaced with a while loop;
 
2) changed the input type from int to long;
 
3) handled decimal input, converting to long int
 
 
*********************************************/

class PrimeFactorizer
{

public void PrimeFactor ( int X)
{
int prime_factor=1; //stores the first prime factor of X
int iLoop=2; //dummy loop counter

if (X<0) //factors out -1 if X<0
{
System.out.print(" -1 " );
X = -X;
}

if ((X>-1) && (X<4)) // nothing to do if X=0,1,2,3 except output
{
System.out.println(X + " "); return;
}


// linear searches for a divisor: stops halfway; if nothing found by then, there aren't any prime factors

prime_factor = 1;

for ( iLoop=2; iLoop<= X/2+1; iLoop++)
{
if ((X % iLoop)==0) // found a prime factor
{
prime_factor=iLoop; //stores it and exits the loop
break;
}

}

if (prime_factor > 1) // prime factor was found
{
System.out.print(prime_factor + " "); // outputs the prime factor
X = X/prime_factor; //divides the prime factor out
PrimeFactor(X); // repeats the process recursively with the prime factor divided out
}
else
{
System.out.println( X); // X is prime, outputs in and ends the output with a new line
}

} //PrimeFactor

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



public void primeFactor( long X)
{

long prime_factor=1; //stores the first prime factor of X
long iLoop=2; //dummy loop counter

if (X<0) //factors out -1 if X<0
{
System.out.print(" -1 " );
X = -X;
}

if ((X>-1) && (X<4)) // nothing to do if X=0,1,2,3 except output
{
System.out.println(X + " "); return;
}

boolean done_flag = false;

while (done_flag==false)
{

// linear searches for a divisor; stops halfway, as if no divisor is found by then, there aren't any

prime_factor = 1;
for (iLoop=2; iLoop< X/2 + 1; iLoop++)
{
if ((X % iLoop)==0) // found a divisor; stores it and exits the loop
{
prime_factor = iLoop;
break;
}

} // for


if (prime_factor > 1) // prime factor was found; outputs it and divides it out
{
System.out.print(prime_factor + " ");
X = X/prime_factor;
}
else //prime factor not found!!! so X is prime; outputs it and sets the done flag
{
System.out.println(X);
done_flag = true;
}

} //while


} //primeFactor




//------------------------------------------------------
public static void main (String args[])
{

long N=0;

if (args.length>0)
{
double dNumInput = Double.parseDouble(args[0]);
if (dNumInput>=0) // converts decimal number to integer
{
N = (long) (Math.floor(dNumInput));
}
else // needs ceiling function if input is negative!! Ex. -34.5 will round to -34 with ceiling but -35 with floor
{
N = (long) (Math.ceil(dNumInput));
}
}
else
{
System.out.println(" Error - no input ");
System.out.println(" command is: java PrimeFactorizer N ");
System.out.println(" where N is the integer to factor ");
System.out.println(" -----------------------------------------");
System.out.println(" EX. java PrimeFactorizer 100 ");
System.exit(1);
return;
}

PrimeFactorizer myPrimeFactorizer = new PrimeFactorizer();
//myPrimeFactorizer.PrimeFactor(N);
myPrimeFactorizer.primeFactor(N);

} //main

} //class PrimeFactorizer
Report

09/17/17

Andy C.

In the second version, I mangled the name, changing upper case P to lower case p
in prime factor() routine.
 
in the main(), commented out the recursive routine in favor of the other.
 
They both work, and they're both there, so you can compare them side by side.
Report

09/17/17

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.