Jordan B.
asked 09/16/17Python coding question
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)
2 Answers By Expert Tutors
Andy C. answered 09/16/17
Math/Physics Tutor
{
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 it and ends the output with a new line
}
} //PrimeFactor
public static void main (String args[])
{
int N=0;
if (args.length>0)
{
N = Integer.parseInt(args[0]);
}
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);
} //main
} //class PrimeFactorizer
Tim C.
09/17/17
Tim C. answered 09/16/17
Experienced and Effective Specialist in GED Math
Andy C.
09/16/17
Andy C.
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
09/17/17
Andy C.
09/17/17
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Andy C.
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);
}
}
09/17/17