File: //opt/perf/examples/arm/jprimenumbers.java
//*******************************************************************
// /opt/perf/examples/arm/jprimenumbers.java
//
// This java program provides an example of how a simple program might
// make use of the ARM java wrappers.
// The program will prompt the user for a prime number, then will
// surround the calculation with an arm_start/arm_stop call. The
// program will output whether the number was prime and the response
// time for the transaction.
//
// Some numbers which will make this think for a while include:
// 27106269116713 (about 18 seconds), 16666666666666661 (about 515 seconds)
//
// Hewlett-Packard Performance Technology Center 11JAN01
import armapi.*;
import java.io.*;
import java.lang.NumberFormatException;
import java.lang.Thread;
public class jprimenumbers {
public static void main(String[] args) throws Exception
{
//
// Initialize ARM with application and transaction IDs
//
ARMApplication armapp = new ARMApplication("Prime_numbers","*");
ARMTransaction Tran = armapp.createTransaction("Prime_Nums");
ARMTransactionInstance traninstance = Tran.createTransactionInstance();
boolean continuing_process = true; // always true
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(input);
String Prime_candidate = new String();
while ( continuing_process )
{
long factor = 3; //current factor being searched
long candidateprime; //number input by user
long factorlimit; //high water for factor to search
long current_time, start_time; //holds time values in millisecs
int elapsed_millisecs; //start minus current time
boolean factorfound = false; //flag to terminate search
System.out.print("Please enter a number (1 to exit): ");
Prime_candidate = buf.readLine();
candidateprime = Long.parseLong(Prime_candidate);
//transaction instance begins once a number has been entered by the user
// start transaction instance
traninstance.startTran();
//start timing
elapsed_millisecs = 0;
start_time = System.currentTimeMillis();
// Check for exit requested:
if (candidateprime == 1)
{
System.out.println("exiting.\n");
traninstance.stopTranInstance(ARMConstants.ARM_ABORT);
System.exit(0);
}
// check to see if reasonable number was input:
if ( candidateprime <= 0 || candidateprime %2 ==0 )
{
System.out.println("Please enter a positive, odd number.\n");
traninstance.stopTranInstance(ARMConstants.ARM_ABORT);
}
else
{
// The prime candidate was a reasonable number, so we'll start
// looking for factors starting from 3 and going up to the
// square root of the candidate.
System.out.println();
factorlimit = (long)java.lang.Math.sqrt(candidateprime);
while( (factor <= factorlimit) && (factorfound == false) )
{
// track current (and thus elapsed milliseconds
current_time = System.currentTimeMillis();
elapsed_millisecs = (int)(current_time - start_time);
// check to see if we're finished
if ( (candidateprime % factor) == 0 ) {
factorfound = true;
}
else { // factor does not divide into candidate, continue search:
factor = factor +2;
} //end else
} // end while searching for factor
// After exiting from the while loop, print result
if ( factorfound == false )
{
System.out.println(candidateprime + " is a prime number.");
}
else
{
System.out.println(candidateprime + " is NOT a prime number.");
System.out.println(candidateprime + " / " + factor + " = " +
candidateprime / factor);
}
traninstance.stopTranInstance(ARMConstants.ARM_GOOD);
System.out.println("Transaction time: " +
elapsed_millisecs / 1000.0 + " seconds.\n");
} // end of else good number
} // end of never-ending while loop
} // end of main
} // end of class jprimenumbers