Thursday 27 January 2011

Calculate prime numbers using PHP

Simply create a file named primeNumberCalculator.php. Copy following text in it.

You need a web server supporting php scripting.
Place the file where your http web server see it. e.g: /var/www

Open the file in a web browser. e.g: http://localhost/primeNumberCalculator.php

<html>
<body>

<h2>PHP Prime number calculator</h2>
<form name="input" action="primeNumberCalculator.php" method="get">
How many primes?: <input type="text" name="count" />
<input type="submit" value="Submit" />
</form>
<?php

// How many prime numbers are we going to calculate:
$count=$_GET["count"];
if ($count == "") {
   $count = 10;  // Default value
}

// We show the first prime number:
$prime_array=array("2");
echo $prime_array[0];
echo " ";

$total=1; // prime number array length.
$number = 3; // current number we are going to test for primality.

$i=1; // We start with 1 because first prime is already printed.
while ( $i<$count ) {
   $index=0;
   $is_prime="true";
   $max = floor(sqrt($number));
   $n = $prime_array[$index];
   while(($n <= $max) && ($is_prime == "true")) {
      if (($number % $n) == 0) {
         $is_prime="false";
      }
      $index++;
      $n = $prime_array[$index];
   }
   if ($is_prime=="true") {
      echo " " . $number;
      $prime_array[$total] = $number;
      $total++;
      $i++;
   }

   $number += 2;
}

?>

</body>
</html>

Wednesday 12 January 2011

Calculate a PRIME NUMBER list using JAVA

This article shows how to write, compile, build and execute a program that calculates a list containing the first prime numbers using Java language.

We are going to create a source code file named CalculatePrimeNumbers.java
Then we copy/paste next programm in that file.

NOTE: Name is important, because in Java language file name has to match the class it contains.

import java.util.ArrayList;

/**
 * Description of CalculatePrimeNumbers class.
 */
public class CalculatePrimeNumbers {

    // By default it shows 50 first prime numbers.
    private static final int MAX_PRIMES = 50;


    /**
     * @param args the command line arguments
     * This function accepts zero or one arguments.
     * When called with no arguments it shows 50 first prime numbers.
     * If we specify a number, it shows that a prime number array with that specified length.
     */
    public static void main(String[] args) {
        int top;

 // Parse command line arguments.
        switch(args.length){
            case 0:
                top = MAX_PRIMES;
                break;
            case 1:
                top = Integer.parseInt(args[0]);
                break;
            default:
                System.err.println("ERROR: TOO MANY ARGUMENTS.");
                return;
        }
        System.out.println("We are going to calculate " + top + " first prime numbers.");


 ArrayList<long> primes = new ArrayList<long>();
        primes.add(2L); // 2 is the first prime number we get.
 long index = 3; // Calculations start for number 3.

        System.out.println("Prime number list:");
        System.out.print(2 + " ");
        while (primes.size() < top) {
            boolean isPrime = true;

     for(long n:primes) {
                if ((index % n) == 0) {
                    isPrime = false;
                    break;
                }
            } // End for.

            if (isPrime) {
                // index stores a prime number.
                primes.add(index);
                System.out.print(index + " ");
            }

            index++;
        } // End while.
       
    } // End main.

}
Java is a programming language that is compiled into bytecodes.