/* Use the Java I/O routines */ import java.io.*; /** * See BIO exam paper. This program solves BIO'97 question 3 part d. * * Solution copyright (c) 1998 The British Informatics Olympiad (BIO). * * This program may be freely copied by persons or organisations * involved in the British Informatics Olympiad or the International * Olympiad in Informatics, on condition that no changes are made and * this notice is not altered. Distribution for profit is forbidden * unless permission is first obtained in writing from the BIO. * * This program is for educational purposes only and comes with no * warranty, implied or otherwise, as to its fitness for any purpose. * * Author: Antony Rix * Internet: http://www.christs.cam.ac.uk/bio/ * E-mail: a.rix@lineone.net * S-mail: The British Informatics Olympiad * Christ's College * Cambridge CB2 3BU * United Kingdom */ class BIO97Q3D extends AppletConsoleApp { /** * Start the application from the command line. */ public static void main(String[] args) { BIO97Q3D thisApp = new BIO97Q3D(); thisApp.redirectStreams(System.in, System.out); thisApp.run(); } /** * Main program of BIO97Q3D, this is run from the command line * by the main() method, or called directly by an * AppletConsole. * * The program reads from the stream in and writes * to the stream out, which are automatically assigned * by main() or by the AppletConsole class. */ public void run() { int a, b; int count = 0; out.println("BIO'97 Q3d:"); out.println("Distinct fractions a/b."); for( b = 2; b < 1000; b++ ) for( a = 1; a < b; a++ ) if (greatestCommonDivisor(a, b) == 1) count++; out.print("Number of distinct fractions for 0 < a < b < 1000 is "); out.println(count); } /** * Returns lowest common factor using Euclid's algorithm. * Requires a < b. */ public int greatestCommonDivisor(int a, int b) { if (a == 0) return b; else return greatestCommonDivisor(b % a, a); } }