// ----------------------------------------------------------------------------------- // /* ROOT macro for calculating the first 1000 prime numbers. Run this ROOT macro from prompt: > root -l PrimeNumbers.c Author: Troels C. Petersen (NBI) Email: petersen@nbi.dk Date: 29th of August 2012 */ // ----------------------------------------------------------------------------------- // //---------------------------------------------------------------------------------- void PrimeNumbers() //---------------------------------------------------------------------------------- { const int NprimeMax = 1000; // Maximum prime number // Loop i over numbers from 2 to NprimeMax: for (int i=2; i < NprimeMax; i++) { bool divisible = false; // Define "divisible" (as false) int MaxDivisor = int(sqrt(i)); // Define "MaxDivisor" // Loop j over numbers from 2 to MaxDivisor: for (int j=2; j < MaxDivisor+1; j++) { if (i%j == 0) divisible = true; // Test if i is divisible by j // ("%" is integer division) } if (!divisible) printf(" %3d ", i); // If i is not divisible, then it is a prime // Print it to the screen using 3 digits } printf("\n"); // Print an "end-of-line" }