import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
public class TrialDivision {
public static void main(String[] args) {
int number = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);
Set<Integer> factors = factorize(number);
System.out.printf("\n\nThere are %d factors for number %d and are : \n",
factors.size(), number);
factors.stream().sorted().forEach(System.out::println);
}
private static int eliminateFactor(int number, int divisor) {
while (number % divisor == 0) {
number = number / divisor;
}
return number;
}
private static boolean storeFactor(int number, int divisor, Set<Integer> factors) {
if (number % divisor == 0) {
factors.add(divisor);
return true;
}
return false;
}
private static Set<Integer> factorize(int number) {
Set<Integer> factors = new HashSet<>();
factors.addAll(Arrays.asList(1, number));
storeFactor(number, 2, factors);
number = eliminateFactor(number, 2);
int divisor = 3;
int sqrt = (int) Math.sqrt(number);
while (divisor <= sqrt) {
boolean isFactor = storeFactor(number, divisor, factors);
number = eliminateFactor(number, divisor);
if (isFactor) {
System.out.printf("divisor : %d, number : %d, sqrt : %d \n", divisor, number, sqrt);
sqrt = (int) Math.sqrt(number);
}
divisor += 2;
}
factors.add(number);
return factors;
}
}
Fundamental Computer Science Algorithm Implementation in Java
Tuesday, May 1, 2018
Trial Division for integers up to Integer.MAX_VALUE in java
Sunday, January 21, 2018
Quick Sort
package sorting;
import java.util.*;
public class QuickSort {
private static List<Integer> sort(List<Integer> data) {
if (data.size() < 2){
return data;
}
List<Integer> lower = new LinkedList<Integer>();
List<Integer> higher = new LinkedList<Integer>();
int pivot = data.get(0);
for (int i = 1; i < data.size(); i++) {
if (data.get(i) < pivot) {
lower.add(data.get(i));
} else {
higher.add(data.get(i));
}
}
sort(lower);
sort(higher);
lower.add(pivot);
lower.addAll(higher);
return lower;
}
public static void main(String[] args) {
List<Integer> unsortedData = new ArrayList<Integer>();
int size = new Random().nextInt(100);
for (int i = 0; i < size; i++) {
unsortedData.add(new Random().nextInt(65536) - 32768);
}
printSort("Unsorted data :", unsortedData);
printSort("Sorted data :", sort(unsortedData));
}
private static void printSort(String message, List<Integer> data) {
System.out.println(message);
for (int item : data) {
System.out.print(item + " ");
}
System.out.println("\n");
}
}
Insertion Sort
package sorting;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class InsertionSort {
private static List<Integer> sort(List<Integer> data) {
List<Integer> sortedData = new LinkedList<Integer>();
outerLoop:
for (int number : data) {
for (int i = 0; i < sortedData.size(); i++) {
if (number < sortedData.get(i)) {
sortedData.add(i, number);
continue outerLoop;
}
}
sortedData.add(sortedData.size(), number);
}
return sortedData;
}
public static void main(String[] args) {
List<Integer> unsortedData = new ArrayList<Integer>();
int size = new Random().nextInt(100);
for (int i = 0; i < size; i++) {
unsortedData.add(new Random().nextInt(65536)-32768);
}
printSort("Unsorted data :", unsortedData);
printSort("Sorted data :", sort(unsortedData));
}
private static void printSort(String message, List<Integer> data) {
System.out.println(message);
for (int item : data) {
System.out.print(item + " ");
}
System.out.println("\n");
}
}
Friday, January 19, 2018
Bubble Sort - java 1.5
package sorting;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class BubbleSort {
private static List<Integer> sort(List<Integer> data) {
boolean swapFlag;
do {
swapFlag = false;
for (int i = 0; i < data.size() - 1; i++) {
if (data.get(i) > data.get(i + 1)) {
int tmp = data.get(i + 1);
data.set(i + 1, data.get(i));
data.set(i, tmp);
swapFlag = true;
}
}
} while (swapFlag);
return data;
}
public static void main(String[] args) {
List<Integer> unsortedData = new ArrayList<Integer>();
int size = new Random().nextInt(100);
for (int i = 0; i < size; i++) {
unsortedData.add(new Random().nextInt(100));
}
printSort("Unsorted data :", unsortedData);
printSort("Sorted data :", sort(unsortedData));
}
private static void printSort(String message, List<Integer> data) {
System.out.println(message);
for (int item : data) {
System.out.print(item + " ");
}
System.out.println("\n");
}
}
Friday, March 21, 2014
Recursion-Fibonacci number generation with caching to prevent exponential time computation
1: package com.mridul.java.algorithms;
2: import java.util.Arrays;
3: import com.mridul.java.helpers.Console;
4: /*
5: * Program to calculate Fibonacci
6: * numbers using Recursion.
7: * This Recursion implements a cache to
8: * increase the speed
9: */
10: public class Recursion {
11: //constants
12: private static final int CACHE_SIZE=100;
13: private static final int MAX_SIZE=92;
14: //static variables
15: private static Recursion rec;
16: private static long[] cache;
17: private static String error_message;
18: /**
19: * Default Constructor
20: */
21: public Recursion(){
22: //create cache and add first two numbers
23: cache=new long[CACHE_SIZE];
24: //initializing array to zero
25: Arrays.fill(cache,0);
26: cache[0]=0;
27: cache[1]=1;
28: error_message=String.format(
29: "Please enter a valid positive integer in the range 0-%d !",
30: MAX_SIZE);
31: }
32: /**
33: * main
34: * @param args
35: */
36: public static void main(String[] args) {
37: try{
38: int size=0;
39: rec= new Recursion();
40: //infinite loop
41: do{
42: System.out.println(
43: "Enter the size of fibonacci numbers: ");
44: size=Integer.parseInt(
45: Console.readFromConsole());
46: if(validate(size)){
47: System.out.println(
48: "The fibonacci sum is : ");
49: System.out.println(rec.computeFibonacci(size));
50: }else{
51: System.out.println(error_message);
52: }
53: }while(size<0);
54: }catch(NumberFormatException ne){
55: System.out.println(error_message);
56: }
57: catch(Throwable t){
58: t.printStackTrace();
59: }
60: }
61: /*
62: * method to validate data
63: */
64: private static boolean validate(int size) {
65: if(size>=0 && size<=MAX_SIZE){
66: return true;
67: }
68: return false;
69: }
70: /*
71: * method which calculates the fibonacci sequence
72: *
73: */
74: private long computeFibonacci(int size) {
75: if(size==0)return 0;
76: if(size==1)return 1;
77: if(cache[size]!=0) return cache[size];
78: cache[size]=computeFibonacci(size-1)+computeFibonacci(size-2);
79: return cache[size];
80: }
81: //End of Recursion
82: }
Subscribe to:
Posts (Atom)