Wednesday, April 22, 2015

Finding all divisors of a number using c#

This is the lazy method of finding all the divisors of a number. I can't say its more efficient way to find the all divisors but it will avoid checking each number.

The divisors of number 28 are,
 
1 * 28 = 28
2 * 14 =28
4 * 7 = 28
7 * 4 = 28
14 * 2 = 28
28 * 1 = 28

Divisors - 1, 2, 4, 7, 14, 28

Checking the divisor's 7, 14, 28 are not required because its already in the existing divisor's quotient.
7 * 4 = 28
14 * 2 = 28
28 * 1 = 28

The number is not possible for having divisors if its greater than sqrt value. Ex:
The sqrt of number 28 is,
5.291502622129181 = 5

The number anything greater than sqrt value will fall under the existing divisor.

1 * 28 = 28
2 * 14 =28
4 * 7 = 28
7 * 4 = 28 => 4 * 7 existing divisor
14 * 2 = 28 => 2 *14 existing divisor
28 * 1 = 28 => 1 *28 existing divisor

How to find all the divisors for a number?
1. Find the sqrt value of the number
2. Loop until the sqrt value
3. Check the number the divisible. If its divisible then find the quotient of the divisor.

The numbers which are divisible and quotients are divisors.


Code
 
    List<int> GetDivisors(int number)
    {
        List<int> lstDivisors = new List<int>();
        double max;

        max = Math.Sqrt(number);

        for (int i = 1; i <= max; i++)
        {
            if (number % i == 0)
            {
                int quotient = number / i;

                lstDivisors.Add(i);
                lstDivisors.Add(quotient);
            }
        }

        return lstDivisors.Select(i=>i).Distinct().ToList();
    }

Sunday, April 12, 2015

Windows 10 phone technical preview - Sim not detected after restart

Some users are facing issues after upgrading to Windows 10 Phone technical preview like sim not detected, phone dial is not opening, new tweet not working in official twitter app etc...

There are two ways to solve these issues,
A. Roll back to Windows phone 8.1
B. Hard reset

I would suggest you to try hard reset if you're hard core windows 10 user.

A. Roll back:
  1. Download and install the Windows Phone Recovery Tool in your desktop
  2. Select your phone and it will download the appropriate installation package (Windows phone 8.1) to your desktop (download size > 1.5 GB)

Read this article for roll back to Windows phone 8.1,
http://www.windowscentral.com/roll-back-windows-phone-81-windows-10-preview

I have seen some of the user comments saying rollback getting failed and screen becomes dark (may be device got bricked).

B. Hard reset:

It will not just reset your device; erase all your phone data and re-install the OS
Try soft reset; Press Volume down + Power button. If it doesnt solved your problem then try hard reset.

Take backup before proceeding the hard reset. Microsoft will restore your backup when setup your device. If you skip the restore process remember you cant do the same again.

Go to Settings -> System -> About -> Reset



Hard reset solved the issues for me. It works.

Thursday, April 9, 2015

How to find prime numbers between 1 to n in c# ?

Read the previous post about prime numbers for knowing the rules and way to identify the number is prime number or not.
http://www.negablog.com/2015/04/how-to-check-number-is-prime-number-or.html

This post will tell you the way to find prime numbers between 1 to N (more than 1+ million) which takes maximum loading time of 500 ms only.

Lets find prime numbers between 1 to N.

"Sieve of Eratosthenes" which is simple and ancient algorithm for finding all prime numbers up to any given limit. Because checking each number for prime takes more loading time.

You can read about "Sieve of Eratosthenes" algorithm in Wikipedia,
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Also give you a quick overview about "Sieve of Eratosthenes" algorithm.

To find all the prime numbers less than or equal to 30, follow the steps.
  1. Identify the prime which multiples (or square) do not exceed 30. (5*5 = 25 < 30)
  2. Generate a list of integers from 2 to 30 (1 is not prime no)
  3. Eliminate the integers which are divisible by 2
  4. Now the list have numbers which are not divisible by 2. Next prime number is 3. Eliminate all the numbers which are divisible by 3.
  5. Next prime number is 5. Eliminate all the numbers which are divisible by 5.
  6. Next prime number is 7. Multiples of number 7 is 14, 21, 28 which are already eliminated. 7*7 is greater than 30. The numbers which remains in the list are all the prime numbers below 30.

Code:

    double FindPrimeNumbers(int startIndex, int endIndex)
    {
        List<int> lstNumbers = new List<int>();
        List<int> lstSievePrimeNumber = new List<int>();
        PrimeNumber primeNumberIdentifier = new PrimeNumber();
        int sieveMaxPrimeNo;


        //  It generates series of numbers in a list for given range
        lstNumbers.AddRange(Enumerable.Range(startIndex, endIndex - 1));


        //  According to "Sieve of Eratosthenes" algorithm, the multiples/square of
        //  number which should not exceed the endIndex
        sieveMaxPrimeNo = Int32.Parse(Math.Round(Math.Sqrt(endIndex), 0).ToString());


        //  Identify the prime number of multiples
        for (int i = sieveMaxPrimeNo; i >= startIndex; i--)
        {
            bool IsPrimeNumber = primeNumberIdentifier.CheckIsPrimeNumber(i);

            if (IsPrimeNumber == true)
            {
                sieveMaxPrimeNo = i;
                break;
            }
        }


        //  Get all the prime numbers between start index and multiples of end index
        for (int i = startIndex; i <= sieveMaxPrimeNo; i++)
        {
            bool IsPrimeNumber = primeNumberIdentifier.CheckIsPrimeNumber(i);

            if (IsPrimeNumber == true)
            {
                lstSievePrimeNumber.Add(i);
            }
        }


        //  Eliminate the numbers which are divisble by prime numbers
        foreach (double sievePrimeNumber in lstSievePrimeNumber)
        {
            lstNumbers.RemoveAll(i => (i % sievePrimeNumber == 0) &&
(i !=sievePrimeNumber));
        }
      

        return lstNumbers;
    }


    List<int> lstPrimeNumbers = FindPrimeNumbers(2, 30);
    //  Output - 2, 3, 5, 7, 11, 13, 17, 19, 23, 29