Monday, March 26, 2012

What is the largest prime factor of the number 600851475143 ?

We solve this program in two stages first we find the factors then we check whether the factor is prime. If its prime then we print the same as a output. Run the loop till all factors have been checked.

The solution given below is running for integers but is not giving any output for really long digits. Still trying to figure out a solution.


/*
What is the largest prime factor of the number 600851475143 ?

*/

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

namespace ConsoleApplication1
{
class Check4Prime
{
public bool checkprime(long a)
{
bool prime = true;
for (long i = a - 1; i > 1; i--)
{
if (a % i == 0)
{
prime = false;
}
}
return prime;
}
}
class Program
{

static void Main(string[] args)
{
long number = 600851475143;
int j=1;

Check4Prime c = new Check4Prime();

for (long factor = number - 1; factor > 1; factor--)
{
if (number % factor == 0)
{
if (c.checkprime(factor))
{
Console.WriteLine(" Prime factor number " + j++ + "of " + number + " is " + factor);
}
}
}

}
}
}

Check whether a number is prime or not

/*
To check whether a number is prime or not

*/

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int number;
Console.WriteLine("Enter the number to be checked for prime");
number = Convert.ToInt32(Console.ReadLine());
bool prime = true;
for (int i = number - 1; i > 1; i--)
{
if (number % i == 0)
{
prime = false;
}
}

if (prime)
{
Console.WriteLine("This number is prime");
}
else
{
Console.WriteLine("This number is not prime");
}

}
}
}


Finding the sum of even valued terms in a fibonacci sequence whose values do not exceed four million


/*
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 four million, find the sum of the even-valued terms.
*/

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int sum=0,f=1, prev1=1, prev2=0;

while( f<4000000)
{
f = prev1 + prev2;
Console.Write(f + ", ");
prev2 = prev1;
prev1 = f;

if (f % 2 == 0)
{
sum = sum + f;
}
}
Console.WriteLine("Sum of all even numbers in a fibonacci sequence where value is less than 4 million is " + sum);
}
}
}

Find the sum of all the multiples of 3 or 5 below 1000

Have started solving various C# problems and am posting the solutions which run on my machine out here on this blog. Please feel free to comment on any errors, mistakes you spot and also in giving any kind of constructive feedback( negative or positive both welcome). (Using Visual Studio .NET 2008 with C#.) Below is the first problem from project euler.

Sum of all the multiples of 3 or 5 below 1000

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int sum=0;

for (int i= 0; i < 1000; i++)
{
if (i % 3 == 0 | i % 5 == 0)
{
sum = sum + i;
}
}
Console.WriteLine("Sum of all natural numbers multiple of 3 or 5 below 1000 is " + sum);
}
}
}