C# Code To Find Circular Prime Numbers between 1 to 100 | URDINESH

Software Programming, Tutorials, Interview Preparations,Stock Market,BSE/NSE, General informations

Sunday, May 18, 2014

C# Code To Find Circular Prime Numbers between 1 to 100

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CircularPrimes
{
    class Program
    {
        public static List<int> findPrimes()
        {
            List<int> res = new List<int>();
            for (int i = 2; i < 100; i++)
            {
                if (i % 2 == 0 && i!=2)
                {

                }
                else
                {
                    if (isPrime(i))
                    {
                        res.Add(i);
                    }
                }
            }
            return res;
        }
        public static bool isPrime(double i)
        {
            for (int j = 3; j <= 7; j++)
            {
                if (i % j == 0 && i != j)
                {
                    return false;
                }
            }
            return true;
        }
        public static List<int> findCircularPrimes(List<int> lstPrimes)
        {
            List<int> res = new List<int>();
            for (int i = 0; i < lstPrimes.Count; i++)
            {
                double num=lstPrimes[i];
                if (lstPrimes[i] < 10)
                {
                   res.Add(lstPrimes[i]);
                }
                else if (lstPrimes[i].ToString().Contains('2') || lstPrimes[i].ToString().Contains('4') || lstPrimes[i].ToString().Contains('6') || lstPrimes[i].ToString().Contains('8') || lstPrimes[i].ToString().Contains('0') || lstPrimes[i].ToString().Contains('5'))
                {
                  
                }
                else
                {
                    bool isCirCulPrime = true;
                    for (int j = 0; j < lstPrimes[i].ToString().Length; j++)
                    {
                        num = Rotate(num, lstPrimes[i].ToString().Length);
                        if(!isPrime(num))
                        {
                            isCirCulPrime = false;
                        }
                    }
                    if (isCirCulPrime)
                    {
                        res.Add(lstPrimes[i]);
                    }
                }
            }
            return res;
        }
        public static double Rotate(double iNumber,int noDigits)
        {
            double givenNo = iNumber;
            int reminder =(int) iNumber % 10;
            int quotient = (int)iNumber / 10;
            return ((reminder*Math.Pow(10,noDigits-1))+quotient);
        }

        static void Main(string[] args)
        {
            List<int> Primes = new List<int>();
            Primes = findPrimes();
            Primes = findCircularPrimes(Primes);
            for (int i = 0; i < Primes.Count; i++)
            {
                Console.WriteLine("{0}", Primes[i]);
            }
            Console.ReadLine();
        }
    }
}


No comments:

Post a Comment

Thanks for your valuable comments

Followers