Hello There, Guest!
View New Posts  |  View Today's Posts
Project Euler Answers Thread

ii 
  • 0 Vote(s) - 0 Average


12-17-2012, 09:10 AM #1
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

Project Euler Answers Thread
Official answers for Project Euler by TLF :) Thought it would be cool to have members contribute here. Don't just post the answer though. Post your code that helped solved the answer if applicable, and don't go Googling the answer to just post here spoiling it for everybody else...

I'll start with a few:
Answer #1
[code=csharp]Console.WriteLine(Enumerable.Range(3, 997).Where(i => i % 3 == 0 || i % 5 == 0).Sum());[/code]
This post was last modified: 01-21-2013, 09:24 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

12-17-2012, 09:14 AM #2
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Project Euler Answers Thread
Answer #2
[code=csharp]private void MainMethod()
{
int T = 0;
int max = 4000000;

int x = 1;
int y = 2;

while (x < max)
{
if (x % 2 == 0) T += x;
Switch(ref x, ref y);
y = x + y;
}
Console.WriteLine(T);
}

public static void Switch(ref int x, ref int y) { int tmp = x; x = y; y = tmp; }[/code]
This post was last modified: 12-17-2012, 09:14 AM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

12-17-2012, 09:27 AM #3
Epixors
(づ。◕‿‿◕。)づ・。*。✧・゜゜・。
*****
TLF Coders
Posts: 420 Threads:58 Joined: Aug 2012 Reputation: 6

RE: Project Euler Answers Thread
Anwer #3
[code=csharp] #region problem3
public static bool Prime(long num)
{
bool prime = false;
if (num != 0 && num != 1)
{
for (long i = 2; i * i <= num; i++)
{
if (num % i == 0) { return false; }
}
prime = true;
}

return prime;
}
public static void Problem3()
{
long num = 600851475143;
double max = Math.Sqrt(600851475143);
long largest = 0;
for (long i = 1; i < max; i++)
{
if((num % i) == 0 && num > largest && Prime(i)){
largest = i;
}
}

Console.WriteLine(largest.ToString());
}
#endregion[/code]

Answer #4
[code=CSHARP] #region problem4


public static void Problem4()
{
string num = "";
string rev = "";
int largest = 0;
for (int i = 100; i <= 999; i++)
{
for (int x = 100; x <= 999; x++)
{
num = (i * x).ToString();
char[] arr = num.ToCharArray();
Array.Reverse(arr);

rev = new string(arr);

if (num == rev)
{
Console.WriteLine(num);
int large = Convert.ToInt32(num);
if (large > largest)
{
largest = large;
}
}

}
}

Console.WriteLine(largest.ToString());
}
#endregion[/code]

On a side note, share your Project Euler friend keys! Mine is: 35318182415111_e03e2383e5fad3d5cd92e9a05500be73

Goto http://projecteuler.net/friends to find and add.
This post was last modified: 12-17-2012, 09:29 AM by Epixors.

12-17-2012, 10:01 AM #4
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Project Euler Answers Thread
(12-17-2012, 09:27 AM)Epixors Wrote:  Anwer #3
[code=csharp] #region problem3
public static bool Prime(long num)
{
bool prime = false;
if (num != 0 && num != 1)
{
for (long i = 2; i * i <= num; i++)
{
if (num % i == 0) { return false; }
}
prime = true;
}

return prime;
}
public static void Problem3()
{
long num = 600851475143;
double max = Math.Sqrt(600851475143);
long largest = 0;
for (long i = 1; i < max; i++)
{
if((num % i) == 0 && num > largest && Prime(i)){
largest = i;
}
}

Console.WriteLine(largest.ToString());
}
#endregion[/code]

Answer #4
[code=CSHARP] #region problem4


public static void Problem4()
{
string num = "";
string rev = "";
int largest = 0;
for (int i = 100; i <= 999; i++)
{
for (int x = 100; x <= 999; x++)
{
num = (i * x).ToString();
char[] arr = num.ToCharArray();
Array.Reverse(arr);

rev = new string(arr);

if (num == rev)
{
Console.WriteLine(num);
int large = Convert.ToInt32(num);
if (large > largest)
{
largest = large;
}
}

}
}

Console.WriteLine(largest.ToString());
}
#endregion[/code]

On a side note, share your Project Euler friend keys! Mine is: 35318182415111_e03e2383e5fad3d5cd92e9a05500be73

Goto http://projecteuler.net/friends to find and add.

That's alright I suppose, I haven't looked at number 4 yet, but for your answer 3, why count upwards??

By counting downwards, the first value you find, will obviously be the highest prime factor, thus you don't need to check for any other value.

Answer #3
[code=csharp]private unsafe void MainMethod()
{
ulong num = 600851475143;
ulong maxnum = (ulong)Math.Sqrt(num);
new Thread(() => ThreadedMethod1(num, maxnum)) { IsBackground = true }.Start();
}

private void ThreadedMethod1(ulong num, ulong maxnum)
{
if (maxnum % 2 == 0) maxnum++;
for (ulong n = maxnum; num > 1; n -= 2)
{
if (num % n == 0 && IsPrime(n))
{
Console.WriteLine(n);
break;
}
}
}

private bool IsPrime(ulong input)
{
if (input == 2) return true;
else if (input == 1 || input % 2 == 0) return false;

ulong limit = (ulong)Math.Sqrt(input + 1);
for (ulong n = 3; n <= limit; n += 2)
if (input % n == 0 && n != input) { return false; }

return true;
}[/code]

I also skip checking even numbers here, and some of my IsPrime function could be removed to make it match what I know will be given as input for the function, but I didn't find that necessary.
This post was last modified: 12-17-2012, 10:04 AM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

12-17-2012, 10:24 AM #5
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Project Euler Answers Thread
For #4 I decided to be lazy and just let a SortedList take care of finding the largest product of two, three digit numbers...

Answer #4
[code=csharp]private unsafe void MainMethod()
{
int min = 100;
int max = 999;

SortedList<int, int> results = new SortedList<int, int>();

for (int i = max; i >= min; i--)
{
for (int x = max; x >= min; x--)
{
int value = i * x;
if (IsPalindrome(value))
{
if (!results.ContainsKey(value)) results.Add(value, value);
}
}
}

//Last value should be highest...
Console.WriteLine(results.Last().Value);
}

private bool IsPalindrome(int i)
{
string s = i.ToString();
return s == new string(s.Reverse().ToArray());
}[/code]


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

12-17-2012, 10:33 AM #6
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Project Euler Answers Thread
Answer #5
[code=csharp]private unsafe void MainMethod()
{
int num = 20;
while (!DivisibleCheck(ref num)) Application.DoEvents();
Console.WriteLine(num);
}

private bool DivisibleCheck(ref int num)
{
for (int i = 20; i >= 1; i--)
{
if (num % i != 0)
{
num += 20;
return false;
}
}
return true;
}[/code]

Here's my answer for #5. Strategy is that, we know if a number is not divisible by 20, then it's not going to matter if we check for 10, or 5, or 2, or any of the other numbers... So we iterate through the multiples of 20.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

12-17-2012, 10:48 AM #7
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Project Euler Answers Thread
Answer #6
[code=csharp]private unsafe void MainMethod()
{
int difference = SquareOfTheSum(100) - SquareOfTheNums(100);
Console.WriteLine(difference);
}

private int SquareOfTheSum(int max)
{
return (int)Math.Pow(Enumerable.Range(1, max).Sum(), 2);
}

private int SquareOfTheNums(int max)
{
return (int)Enumerable.Range(1, max).Select(i => Math.Pow(i, 2)).Sum();
}[/code]

Problem #6 was actually really simple with this LINQ lol... Easiest question yet almost.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

12-17-2012, 11:04 AM #8
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Project Euler Answers Thread
Answer #7
[code=csharp]private unsafe void MainMethod()
{
int primeCount = 1; //Assume we already accounted for 2 being prime
int i = 3;

while (primeCount < 10001)
{
if (IsPrime(i))
{
primeCount++;
}
if (primeCount != 10001) i += 2;
}
Console.WriteLine(i);
}

private bool IsPrime(int input)
{
if (input == 2) return true;
else if (input == 1 || input % 2 == 0) return false;

int limit = (int)Math.Sqrt(input + 1);
for (int n = 3; n <= limit; n += 2)
if (input % n == 0 && n != input) { return false; }

return true;
}[/code]

Problem #7 was another fairly easy one. I wasn't sure how I wanted to structure the logic for this one.. Although this one seems to do the trick fast enough for me, so I didn't bother with changing anything here.
This post was last modified: 12-17-2012, 11:05 AM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

12-17-2012, 11:10 AM #9
KoBE
¯\_(ツ)_/¯
******
Global Moderators
Posts: 4,862 Threads:494 Joined: Jun 2011 Reputation: 67

RE: Project Euler Answers Thread
My friend key: 36612120415272_51464f31c43eae2f6d1035be7456b5ed

Using Ace's IsPrime() function:

#7
Code:
static void Main(string[] args)
{
    int count = 0;
    ulong itr = 0;
    while (count != 10001)
    {
        itr++;
        if (IsPrime(itr)) count++;
    }
    Console.WriteLine(itr);
    Console.ReadKey();
}
static bool IsPrime(ulong input)
{
    if (input == 2) return true;
    else if (input == 1 || input % 2 == 0) return false;

    ulong limit = (ulong)Math.Sqrt(input + 1);
    for (ulong n = 3; n <= limit; n += 2)
        if (input % n == 0 && n != input) { return false; }

    return true;
}
This post was last modified: 12-17-2012, 11:10 AM by KoBE.

12-17-2012, 11:15 AM #10
Epixors
(づ。◕‿‿◕。)づ・。*。✧・゜゜・。
*****
TLF Coders
Posts: 420 Threads:58 Joined: Aug 2012 Reputation: 6

RE: Project Euler Answers Thread
Answer #7
Code:
#region problem7
        public static void Problem7()
        {
            List<long> primes = new List<long>();
            int i = 0;

            while (primes.Count != 10001)
            {
                Console.WriteLine(i);
                if (Prime(i)) { primes.Add(i); Console.WriteLine(i); }
                i++;
            }
        }
        #endregion



My Prime function is:

Code:
public static bool Prime(long num)
        {
            bool prime = false;
            if (num != 0 && num != 1)
            {
                for (long i = 2; i * i <= num; i++)
                {
                    if (num % i == 0) { return false; }
                }
                prime = true;
            }

            return prime;
        }

I'm at Problem 12 now!
This post was last modified: 12-17-2012, 11:16 AM by Epixors.


ii 


Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
  MMIX <- Virtual Motherboard Project AceInfinity 0 1,240 10-04-2013, 08:50 PM
Last Post: AceInfinity
  The Recursive Thread! AceInfinity 5 2,478 08-24-2013, 12:55 PM
Last Post: AceInfinity
  Project Euler #418 AceInfinity 2 3,221 03-13-2013, 09:13 PM
Last Post: AceInfinity
  MODx Revolution - My new friend (SEO project) KoBE 10 6,026 01-06-2013, 03:17 AM
Last Post: Adriana
  Project Euler - "Firecracker" (Problem 317) AceInfinity 0 2,679 12-22-2012, 11:35 PM
Last Post: AceInfinity


Users browsing this thread: 1 Guest(s)