Sunday 22 May 2016

An editorial on function to find the prime number with least time complexity :)

  • The algorithm can be improved further by observing that all primes are of the form 6m ± 1, with the exception of 2 and 3.
  • This is because all integers can be expressed as (6m + i) for some integer k and for i = −1, 0, 1, 2, 3, or 4; 2 divides (6m + 0), (6m + 2), (6m + 4); and 3 divides (6m + 3).
  • So a more efficient method is to test if n is divisible by 2 or 3, then to check through all the numbers of form 6m ± 1 ≤ √n(sqrt(n)).
  • This is 3 times as fast as testing all m up to √n.
    int CheckPrime(unsigned int number) {
        if (number <= 3 && number > 1) 
            return 1;            // as 2 and 3 are prime
        else if (number%2==0 || number%3==0) 
            return 0;     // check if number is divisible by 2 or 3
        else {
            unsigned int i;
            for (i=5; i*i<=number; i+=6) {
                if (number % i == 0 || number%(i + 2) == 0) 
                    return 0;
            }
            return 1; 
        }
    }
  • This code is written in C, in java we don't have unsigned keyword(however in java 8 it is included). 

Wednesday 11 May 2016

Converting char (Primitive Character) to String in Java.

Hi, Today I'm going to tell you 3 ways to convert a primitive char to a String.

First Method:
This one is the simplest a person can think.
String str = "" + 'c';

This internally compiles down to :
String str = new StringBuilder().append("").append('c').toString();

This one is less efficient due to StringBuilder class.

Second Method:
As we cannot invoke toString() on the primitive type char or any other primitive, so we  can create a wrapper class Character and then we can eaily invoke toString() to it.
ex.
char c = 'c';
Character ch = Character.valueOf(c);
String str = ch.toString();

Third Method:
char c = 'c';
String str = String.valueOf(c);

Thanks

Tuesday 10 May 2016

Installing Python3.5.1 on fedora 23

Okay, here I'm going to tell how to install Python 3.5.1 using the source tar and compiling it. Using package manager is a great idea but it doesn't seem to work for me always (it will work surely in ubuntu). Sometime I try this for fedora then it cause me problem. So here I will tell you how to install python3.5.1 by compiling the source.


  • Download the python3.5.1 tar file.
  • Extract it.
  • Change Directory to the Python-3.5.1
  • write './configure' in terminal
  • write 'make' in terminal after the above command executes.
  • write 'make install' in terminal after the above command executes.


And that's it.


Enjoy :)

Bug removed for fedora : Unable to run mksdcard SDK tool

If you are directly installing Android Studio on Fedora or any other Linux system then while installing SDK it gives an error message : 

Unable to run mksdcard SDK tool

This is one big bug and people can easily find its solution for ubuntu but for fedora you need to different libraries to install .
Just copy paste the command below in your terminal and this will solve your problem :

sudo dnf install compat-libstdc++-296 compat-libstdc++-33 glibc libgcc nss-softokn-freebl libstdc++ ncurses-libs zlib-devel.i686 ncurses-devel.i686 ant

Sunday 8 May 2016

Reference variable of parent class can access the overridden method in child class.

Hi,
As we know that the reference variable of parent class storing the object of child class can only access the members of its own class(i.e. the parent class). But you should keep in mind that it can access the overridden method in the child class.

The famous example is given here on oracle docs : - click here

I'm pasting that code snippet here also : -
public class Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Animal");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Animal");
    }
}
//The second class, a subclass of Animal, is called Cat:

public class Cat extends Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Cat");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Cat");
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        Animal myAnimal = myCat;
        Animal.testClassMethod();
        myAnimal.testInstanceMethod();
    }
}

The output is : 


The static method in Animal
The instance method in Cat

This proves.