素因数分解してみる
戻る
$ java PrimeFactors 1234523732 1784199999 325217 1983 493284 68886091 543175 8415965
1234523732 は右記の素因数で構成されています。[ 2 2 13 73 325217 ]
1784199999 は右記の素因数で構成されています。[ 3 23 89 290539 ]
325217 は右記の素因数で構成されています。[ 325217 ]
1983 は右記の素因数で構成されています。[ 3 661 ]
493284 は右記の素因数で構成されています。[ 2 2 3 11 37 101 ]
68886091 は右記の素因数で構成されています。[ 17 103 39341 ]
543175 は右記の素因数で構成されています。[ 5 5 21727 ]
8415965 は右記の素因数で構成されています。[ 5 167 10079 ]
import java.io.*;
import java.util.*;
/**
$Id: PrimeFactors.html,v 1.1 2009/06/22 16:11:52 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public class PrimeFactors {
private List factors;
private long num;
public PrimeFactors( long num ) {
this.num = num;
calc();
}
/** 力ずくのタコロジックで計算する(^^)*/
private void calc() {
factors = new LinkedList();
long targetIntValue = num;
for ( long i = 2;i < targetIntValue;i++ ) {
while ( targetIntValue % i == 0 ) {
factors.add( i );
targetIntValue /= i;
}
}
/** 1以外は表示しない */
if ( targetIntValue != 1 ) {
factors.add( targetIntValue );
}
}
public List getFactors() {
return factors;
}
public void showList() {
Iterator iterator = factors.iterator();
System.out.print( num + "\tは右記の素因数で構成されています。[ " );
while ( iterator.hasNext() ) {
System.out.print( iterator.next() + " " );
}
System.out.println( "]" );
}
public static void main( String[] args ) {
for ( int i = 0;i < args.length;i++ ) {
PrimeFactors pf = new PrimeFactors( new Integer( args[ i ] ).longValue() );
pf.showList();
}
}
}
戻る