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;
    }