Tuesday, 2 February 2016

Project Euler #5 : Smallest multiple

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Solution
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
         int testCases = Convert.ToInt32(Console.ReadLine());
            int[] numbers = new int[testCases];
            for (int i = 0; i < testCases; i++)
            {
                numbers[i] = Convert.ToInt32(Console.ReadLine());
            }
            foreach (int number in numbers)
            {
                long product = 1;
                for (int i = 2; i <= number; i++)
                {
                    if(IsPrime(i))
                    {
                        int temp = i;
                        for(int j=2;j<=number;j++)
                        {
                            if(Math.Pow(Convert.ToDouble(i),Convert.ToDouble(j))>number)
                            {
                                break;
                            }
                            else
                            {
                                temp = Convert.ToInt32(Math.Pow(Convert.ToDouble(i), Convert.ToDouble(j)));
                            }
                        }
                        product = product * temp;
                    }
                }
                Console.WriteLine(product);
            }
    }
    public static bool IsPrime(int number)
        {
            if (number == 2 || number == 3)
                return true;
            else
            {
                bool flag = true;
                for (int i = 2; i <= Math.Sqrt(number); i++)
                {
                    if (number % i == 0)
                    {
                        flag = false;
                        break;
                    }
                }
                return flag;
            }
        }
}

Project Euler #4 : Largest palindrome product

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers

Solution

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
         int testCases = Convert.ToInt32(Console.ReadLine());
            List<int> numbers = new List<int>();
            for (int k = 0; k < testCases; k++)
            {
                numbers.Add(Convert.ToInt32(Console.ReadLine()));
            }
            foreach (int number in numbers)
            {
                Console.WriteLine(FindLargestNumber(number));
            }
    }
    public static int FindLargestNumber(int number)
        {
            for (int x = 9; x >= 0; --x)
            {
                int a = x * 100001;
                for (int y = 9; y >= 0; --y)
                {
                    int b = a + y * 10010;
                    for (int z = 9; z >= 0; --z)
                    {
                        int n = b + z * 1100;
                        if (n < number)
                        {
                            for (int i = 990; i > 99; i -= 11)
                            {
                                if (n % i == 0)
                                {
                                    int t = n / i;
                                    if (t < 1000) return n;
                                }
                            }
                        }
                    }
                }
            }
            return 0;
        }
}

Friday, 16 October 2015

Project Euler #3: Largest prime factor

Today I will show how to solve Project Euler Problem 3
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of a given number N?
Input Format
First line contains T, the number of test cases. This is followed by T lines each containing an integer N.
Output Format
For each test case, display the largest prime factor of N.
Constraints
1≤T≤10
10≤N≤1012
Sample Input
2
10
17
Sample Output
5
17

Solution
int T = int.Parse(Console.ReadLine());
long[] numbers = new long[T];
for (int i = 0; i < T; i++)
{
    numbers[i] = Int64.Parse(Console.ReadLine());
}
for (int i = 0; i < T; i++)
{
    long num = numbers[i];
    int a = 3;
    while (num % 2 == 0)
      num = num / 2;
    if (num == 1)
      num = 2;
    while (a <= Math.Sqrt(num))
    {
      if (num % a == 0)
      {
        num = num / a;
        continue;
                        //a --;
      }
        a = a + 2;
    }
      Console.WriteLine(num);
}


Wednesday, 14 October 2015

Project Euler #2: Even Fibonacci numbers

Hi folks, today I will show you how to solve Project Euler Problem 2.
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1,2,3,5,8,13,21,34,55,89,
By considering the terms in the Fibonacci sequence whose values do not exceed N, find the sum of the even-valued terms.
Solution

int i = Convert.ToInt32(Console.ReadLine());
List<decimal> list = new List<decimal>();
for (int j = 0; j < i; j++)
{
   list.Add(Convert.ToInt64(Console.ReadLine()));
}
foreach (decimal k in list)
{
   long b = 1;
   long c = 2, d;
   long sum = 0;
   while (c < k)
   {
      sum += c;
      d = b + (c << 0x01);
      c = d + b + c;
      b = d;
   }
   Console.WriteLine(sum);
}

Project Euler #1: Multiples of 3 and 5

Hi guys, today I will show you how to solve Project Euler Problem 1, the question is
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000000000.

I tried to solve this question using for loop. It was working fine but it was taking so many times to get the result. Then I implemented some mathematical logic to solve this issue.


static void Main(String[] args) {
            int i = Convert.ToInt32(Console.ReadLine());
            List<long> list = new List<long>();
            for (int j = 0; j < i; j++)
            {
                list.Add(Convert.ToInt64(Console.ReadLine()));
            }
            foreach(long k in list)
            {
                long sum=0;
                long i3 = (((k - 1) / 3) * (((k - 1) / 3) + 1)) / 2;
                sum += i3 * 3;
                long i5 = (((k - 1) / 5) * (((k - 1) / 5) + 1)) / 2;
                sum += i5 * 5;
                long i15 = (((k - 1) / 15) * (((k - 1) / 15) + 1)) / 2;
                sum -= i15 * 15;
                Console.WriteLine(sum);
            }
    }

How to clear the values entered in the textbox by pressing F5 or Refreshing the page

Hi guys, today I will show you how to clear the textbox value when you press F5, This will also prevent you from submission of data when you press F5.

First create a design page like this
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       First Name :  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
       Last Name : <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
       <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Now write the following code in aspx.cs page

protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Session["Submitted"] = false;
        }
        if(Convert.ToBoolean(Session["submitted"].ToString())==true)
        {
            TextBox1.Text = string.Empty;
            TextBox2.Text = string.Empty;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["Submitted"] = true;
    }

Saturday, 22 November 2014

Linq to XML Example 1



Hi guys, today I will show you one demo on LINQ to XML, this is very very helpful for the beginners who are trying to learn LINQ. Here I have prepared one demo like I have a xml file with list of employees, when a user give the employee ID it should populate the employee Name.
First Create a XML File
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
  <Employee>
    <ID>001</ID>
    <Name>Rohit</Name>
  </Employee>
  <Employee>
    <ID>002</ID>
    <Name>Sam</Name>
  </Employee>
  <Employee>
    <ID>003</ID>
    <Name>Jonshon</Name>
  </Employee>
  <Employee>
    <ID>004</ID>
    <Name>Rocky</Name>
  </Employee>
</Employees>

Then design the page like this
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        Enter ID :
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
   
    </div>
    </form>
</body>
</html>

Now write the following code in the aspx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        XElement obj = XElement.Load(Server.MapPath("~/XMLFile.xml"));
      
       string employeeName= obj.Descendants("Employee")
                            .Where(x => (string)x.Element("ID") == TextBox1.Text)
                            .Select(x=>(string)x.Element("Name")).SingleOrDefault();
       if (!string.IsNullOrEmpty(employeeName))
       {
           Label1.Text = employeeName;
       }
       else
       {
           Label1.Text = "No Employee";
       }
    }
}

Demo