Tuesday, October 14, 2008

http://projecteuler.net Problem 2

This problem asked to sum up all even Fibanacci number up to 4,000,000. This code executed in .02 seconds


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProjectEularProb2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
DateTime dtstart = DateTime.Now;
List numb = new List();
List lastNumb = new List();
int x = 1;
int y=0;
//numb.Add(x);
listBox1.Items.Add(x);
lastNumb.Add(1);

do
{
y = lastNumb.Last();
x = x + y;
listBox1.Items.Add(x);
if (IsEven(x).Equals(true))
{
numb.Add(x);
listBox2.Items.Add(x);
}

lastNumb.Add(x - y);
}
while (x <= 4000000);
textBox1.Text = numb.Sum().ToString();
DateTime dtend = DateTime.Now;
label3.Text = "Program took " + dtend.Subtract(dtstart) + " to complete";
}
private bool IsEven(int x)
{
int y = x % 2;
if (y==0)
{
return true;
}
else
{
return false;
}
}
}
}

Monday, October 13, 2008

http://projecteuler.net Problem 1

So I found this cool website that I think will keep me entertained for a while called http://projecteuler.net

I was able to solve Problem 1 pretty quickly. I am going to use C# to solve these problems. It is probably not the most elegant code, but if you know me that probably does not surprise... Here is the source to problem 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProjectEularProb1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

List numlist = new List();
bool alreadyThere = false;

private void button1_Click(object sender, EventArgs e)
{

List numbs = new List();

numbs.Add(3);
numbs.Add(5);

foreach (int x in numbs)
{
FindMultiplesUnderAG(x);
}

textBox1.Text = numlist.Sum().ToString();
}
private void FindMultiplesUnderAG(int x)
{
int y = 0;

for (int i = 0; i <= 1000; i++)
{
y = i * x;
if (y < 1000)
{
foreach (int z in numlist)
{
if (y == z)
{
alreadyThere = true;
}
}
if(alreadyThere.Equals(false))
{
numlist.Add(y);
listBox1.Items.Add(y);
}
alreadyThere = false;
}
}
}

private void button2_Click(object sender, EventArgs e)
{
textBox1.Clear();
listBox1.Items.Clear();
numlist.Clear();
}
}
}

How to rename every file in a directory from a command prompt

Found this on another site (forgot where) Just in case I need this in the future

Create a .bat file and paste in

for %%f in (_*.*) do call :renamer %%f
goto :eof

:renamer
set old=%1
echo %1
ren %1 %old:~1,9%
:: end

in line 1 the _ is the character pattern to match
on line 6 change ~1 to the number of characters from line 1

So its been a while...

Thought I would re-purpose this blog to be more of a personal notes site. We'll see how this goes