Prime Number Finder In Java

A prime number (or a prime) is a natural number which has exactly two distinct natural number divisors: 1 and itself.
This small program runs through 2-500 and print all the prime numbers in that range.

Note: The number 1 is by definition not a prime number.

package com.kushal.utils;

public class PrimeNumberInJava {
	/**
	 * Method to test whether a number is
	 * a prime number or not
	 */
	public static boolean isPrimeNumber(int number) {
		boolean prime = true;
		int limit = (int) Math.sqrt(number);

		for (int i = 2; i <= limit; i++) {
			if (number % i == 0) {
				prime = false;
				break;
			}
		}

		return prime;
	}

	public static void main(String[] args) {

		/**
		 * Loop From 2-500 and Print all the prime numbers
		 */
		System.out.println("List Of Prime Numbers From 2-500");
		for (int i = 2; i <= 500; i++) {
			if (isPrimeNumber(i))
			{
				System.out.println(i);
			}
		}
	}
}

———————————
Here is the output of this program:

List Of Prime Numbers From 2-500
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229
233
239
241
251
257
263
269
271
277
281
283
293
307
311
313
317
331
337
347
349
353
359
367
373
379
383
389
397
401
409
419
421
431
433
439
443
449
457
461
463
467
479
487
491
499

Printing Fibonacci Sequence Using Recursive and Iterative Methods

By definition, the first two numbers in the Fibonacci sequence are 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

1, 1, 2, 3, 5, 8, 13, 21, 34…
or
0, 1, 2, 3, 5, 8, 13, 21, 34…

Programmatically Fibonacci Sequence can be computed both iteratively and recursively. In the following Java program, I have shown how to print n numbers in the sequence using both approach. Please note that I have generated 1 based sequence, not the 0 based.

package com.icodejava.blog.published;

/**
 * @author Kushal Paudyal
 * www.icodejava.com
 * Created On -  Mar 11, 2014
 * Last Modified On - Mar 11, 2014
 */
public class Fibonacci {

	static final int NUMBER_OF_FIBONACCI_SEQUENCE = 10;

	public static void main(String args[]) {

		System.out.print("RECURSIVELY GENERATED:");
		generateFibonacciRecursive(NUMBER_OF_FIBONACCI_SEQUENCE);

		System.out.print("\nITERATIVELY GENERATED:");
		generateFibonacciIterative(NUMBER_OF_FIBONACCI_SEQUENCE);
	}

	/**
	 * Loops n number of times to generate Fibonacci Sequence.
	 * Each nth Fibonacci Number is generated iteratively.
	 */
	private static void generateFibonacciIterative(int n) {
		for (int i = 0; i < n; i++) {
			System.out.print(fibIterative(i) + ",");
		}

	}

	/**
	 * Loops n number of times to generate Fibonacci Sequence.
	 * Each nth Fibonacci Number is generated recursively.
	 */
	private static void generateFibonacciRecursive(int n) {
		for (int i = 0; i < n; i++) {
			System.out.print(fibRecursive(i) + ",");
		}

	}

	/**
	 * Uses recursion to generate nth Fibonacci number
	 */
	private static int fibRecursive(int n) {
		int result = -1;
		if (n == 0 || n == 1) {
			result = 1;
		} else if (n > 1) {
			result = fibRecursive(n - 1) + fibRecursive(n - 2);

		}

		return result;
	}

	/**
	 * User iteration to generate nth Fibonacci number
	 */
	private static int fibIterative(int n) {
		if (n < 0) {
			return -1;
		}

		if (n == 0) {
			return 1;
		}

		int a = 1;
		int b = 1;

		for (int i = 2; i <= n; i++) {
			int c = a + b;
			a = b;
			b = c;
		}

		return b;
	}

}

Here is the output of running above program:

RECURSIVELY GENERATED:1,1,2,3,5,8,13,21,34,55,
ITERATIVELY GENERATED:1,1,2,3,5,8,13,21,34,55,

Finding Square Root Of A Double Number In Java Using Binary Search

The following program uses binary search to effectively find the square root of the number. Given a number, we start dividing it into half and see if the mid value multiplied to itself constitutes the original number (i.e. originalNumber = midValue * midValue). If the square of the midValue is greater than the originalNumber, we start looking into the left half of the number, if it is smaller, we start looking into the right half.

Since we are allowing double numbers as parameter, we are using a double precision to compare.

If the binary search does not exactly find the number, the approximated value of the square root of the half of last known start and end.

package com.icodejava.blog;

/**
 * @author Kushal Paudyal
 * www.icodejava.com
 * Created On -  Mar 11, 2014
 * Last Modified On - Mar 11, 2014
 */
public class SquareRootFinder {

	public static void main(String args[]) {

		System.out.println("Square Root of 25 is: " + squareRoot(25));
		System.out.println("Square Root of 81 is: " + squareRoot(81));
		System.out.println("Square Root of -100 is: " + squareRoot(-100));
		System.out.println("Square Root of 1 is: " + squareRoot(1));
		System.out.println("Square Root of 0 is: " + squareRoot(0));

	}

	/**
	 * This method use binary search to find the square root.
	 * 
	 * @param number
	 * @return square root of the number
	 */
	private static double squareRoot(double number) {
		double squareRoot = 0;
		double startValue = 0;
		double endValue = number;
		double precision = 0.00001;

		if (number < 0) {
			squareRoot = -1;
		} else if (number == 0 || number == 1) {
			squareRoot = number;
		} else {

			// we will use binary search to narrow down.
			while (endValue - startValue > precision) {
				double midValue = (startValue + endValue) / 2;
				squareRoot = midValue * midValue;

				if (squareRoot == number) {
					return squareRoot;
				} else if (squareRoot > number) {
					endValue = midValue;
				} else {
					startValue = midValue;
				}
			}

			// if a match is not found
			squareRoot = (startValue + endValue) / 2;

		}
		return squareRoot;
	}

}

Here is the output of running the above program:

Square Root of 25 is: 4.999998211860657
Square Root of 81 is: 8.999999463558197
Square Root of -100 is: -1.0
Square Root of 1 is: 1.0
Square Root of 0 is: 0.0