Monday, November 3, 2014

Java prime numbers



Write a program to find the sum of the first 1000 prime numbers

package com.primesum;
public class Main
{
public static void main(String args[])
{
        int number = 2;
        int count = 0;
        long sum = 0;
        while(count < 1000)
        {
            if(isPrimeNumber(number))
            {
                sum += number;
                count++;
            }
            number++;
        }
        System.out.println(sum);
    }
    private static boolean isPrimeNumber(int number)
    {
    for(int i=2; i<=number/2; i++)
    {
            if(number % i == 0)
            {
                return false;
            }
        }
        return true;
    }
}

Find longest substring without repeating characters

package com.java2novice.algos;
import java.util.HashSet;
import java.util.Set;
public class MyLongestSubstr
    private Set subStrList = new HashSet();
    private int finalSubStrSize = 0;
public Set getLongestSubstr(String input)
{
        subStrList.clear();
        finalSubStrSize = 0;
        boolean[] flag = new boolean[256];
        int j = 0;
        char[] inputCharArr = input.toCharArray();
        for (int i = 0; i < inputCharArr.length; i++)
        {
            char c = inputCharArr[i];
            if (flag[c])
             {
                extractSubString(inputCharArr,j,i);
                for (int k = j; k < i; k++)
                 {
                    if (inputCharArr[k] == c)
                    {
                        j = k + 1;
                        break;
                    }
                    flag[inputCharArr[k]] = false;
                }  
            }
            else
            {
                flag[c] = true;
            }
        }
        extractSubString(inputCharArr,j,inputCharArr.length);
        return subStrList;
    }
    private String extractSubString(char[] inputArr, int start, int end)
    {
        StringBuilder sb = new StringBuilder();
        for(int i=start;i
        {
            sb.append(inputArr[i]);
        }
        String subStr = sb.toString();
        if(subStr.length() > finalSubStrSize)
        {
            finalSubStrSize = subStr.length();
            subStrList.clear();
            subStrList.add(subStr);
        }
        else if(subStr.length() == finalSubStrSize)
        {
            subStrList.add(subStr);
        }
         
        return sb.toString();
    }
     public static void main(String a[])
     {
        MyLongestSubstr mls = new MyLongestSubstr();
        System.out.println(mls.getLongestSubstr("java2novice"));
        System.out.println(mls.getLongestSubstr("java_language_is_sweet"));
        System.out.println(mls.getLongestSubstr("java_java_java_java"));
        System.out.println(mls.getLongestSubstr("abcabcbb"));
    }
}

Write a program to remove duplicates from sorted array

package com.java2novice.algos;
public class MyDuplicateElements
{
public static int[] removeDuplicates(int[] input)
{
        int j = 0;
        int i = 1;
        if(input.length < 2)
        {
            return input;
         }
        while(i < input.length)
         {
            if(input[i] == input[j])
            {
                i++;
            }else{
                input[++j] = input[i++];
            }   
        }
        int[] output = new int[j+1];
        for(int k=0; k
        {
            output[k] = input[k];
        }
         
        return output;
    }
   public static void main(String a[])
   {
        int[] input1 = {2,3,6,6,8,9,10,10,10,12,12};
        int[] output = removeDuplicates(input1);
        for(int i:output)
         {
            System.out.print(i+" ");
        }
    }
}

Simple JUnit test using Test annotation

package com.java2novice.junit.tests;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.java2novice.junit.samples.MyEvenOdd;
public class MyEvenOddTest
{
 public void testEvenOddNumber()
{
        MyEvenOdd meo = new MyEvenOdd();
        assertEquals("10 is a even number", true, meo.isEvenNumber(10));
    }
}

No comments:

Post a Comment