Problem #145 says:
Some positive integers n have the property that the sum [ n + reverse(n) ] consists entirely of odd (decimal) digits. For instance, 36 + 63 = 99 and 409 + 904 = 1313. We will call such numbers reversible; so 36, 63, 409, and 904 are reversible. Leading zeroes are not allowed in either n or reverse(n).
There are 120 reversible numbers below one-thousand.
How many reversible numbers are there below one-billion (10^(9))?
The solution below is c# with .Net 4.0. It does a very basic divide and conquer on the one billion numbers to check. This will take a while to run depending on your computer, it ran reasonably fast on a quad core machine. Pretty simple stuff could be made much faster but was written out of boredom.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Parallel;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
namespace Net4Test
{
class Program
{
static object LockHandle = new object();
static long Answer = 0;
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Parallel.For(1L, 1000000000L, i =>
{
if (i % 50000 == 0)
Console.WriteLine(i);
if (Reversable(i))
{
lock (LockHandle)
{
Answer += 1;
}
}
});
Console.WriteLine(Answer);
sw.Stop();
Console.WriteLine(sw.Elapsed);
Console.ReadLine();
}
static bool Reversable(long l)
{
bool RetValue = true;
char[] lchar = l.ToString().ToCharArray();
Array.Reverse(lchar);
if (lchar[0] != '0')
{
long Reversel = (long)Convert.ToDouble(new string(lchar));
char[] AChar = (l + Reversel).ToString().ToCharArray();
foreach (char c in AChar)
{
if (!IsOdd(Convert.ToInt32(c.ToString())))
RetValue = false;
}
}
else
RetValue = false;
return RetValue;
}
static bool IsOdd(int i)
{
if (i == 0)
return false;
if (i % 2 == 0)
return false;
else
return true;
}
}
}
Tags: c#, Math, programming, project euler
Posted in Project Euler | Comments (0)
Problem #206 says:
Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0,
where each “_” is a single digit.
The range of number to check can be narrowed down with a calculator and some common sense. Then its just a square and check procedure. Solution is in c# and requires the .Net 4.0 framework for its parallel extensions.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Parallel;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Numerics;
using System.Text.RegularExpressions;
namespace Net4Test
{
class Program
{
static object LockHandle = new object();
static long Answer = 0;
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Regex Test = new Regex(
"[1][0-9][2][0-9][3][0-9][4][0-9]" +
"[5][0-9][6][0-9][7][0-9][8][0-9][9][0-9][0]");
Parallel.For(1360000000, 1390000000, i =>
{
BigInteger biI = i;
if (Test.IsMatch((biI * biI).ToString()))
{
Answer = i;
}
});
Console.WriteLine(Answer);
sw.Stop();
Console.WriteLine(sw.Elapsed);
Console.ReadLine();
}
}
}
Tags: c#, csharp, project euler
Posted in Project Euler | Comments (0)
(updates on the site. pagerank just hit 3 on google. hopefully this will increase traffic a bit. and this post is the first post on the site done in windows 7 (took 6 hours for my pc to update
)
Problem #50 says:
The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?
The solution below is pretty much brute force. We generate the primes and then add them together and subtract from the total items in the list of primes. then we get the total of all of the primes minus the first item in the primes list and do it over again. there’s lots of room for improvements and when I get the time I’ll post them. but for now this is what I used to get the solution. Solution provided in c#/mono.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ProjectEuler
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
ArrayList primes = GeneratePrimes(1000000);
primes.Remove(1);
long result = 0;
long maxlength = 0;
Console.WriteLine("starting check");
for (int start = 0;start<primes.Count;start++)
{
long temp = 0;
for (int i = start; i < primes.Count; i++)
{
temp = temp + (long)primes[i];
}
for (int k = primes.Count - 1; k > start; k--)
{
if (temp < 1000000 && primes.Contains(temp) && k - start >= maxlength)
{
maxlength = k -start;
result = temp;
Console.WriteLine(result);
break;
}
temp = temp - (long)primes[k];
if (temp <= start)
break;
}
}
Console.WriteLine(result);
Console.WriteLine(maxlength);
Console.WriteLine(sw.Elapsed);
Console.ReadLine();
}
static ArrayList GeneratePrimes(long num)
{
ArrayList RetValue = new ArrayList();
RetValue.Add((long)2);
RetValue.Add((long)3);
long Stepper = 5;
long Check = 1;
while (Stepper <= num)
{
foreach (long i in RetValue)
{
if (Stepper % i == 0)
{
Check = 0;
break;
}
if (Math.Sqrt(Stepper) < i)
{
break;
}
}
if (Check == 1)
{
//Console.WriteLine(((float)Stepper / (float)num) * 100);
RetValue.Add(Stepper);
}
Check = 1;
Stepper++;
Stepper++;
}
return RetValue;
}
}
}
Tags: c#, csharp, mono, programming, project euler
Posted in Project Euler | Comments (0)
So in my recent works with Gtk I’ve come across (and written) some useful snippets so I thought I’d post a few of them.
The first comes from this site (its in Spanish), this snippet allows you to take a System.Drawing.Image to a Gdk.Pixbuf and it looks like this (slightly modified to fix an ambiguous reference in my project).
static Gdk.Pixbuf ImageToPixbuf( System.Drawing.Image image )
{
using ( MemoryStream stream = new MemoryStream() )
{
image.Save( stream, System.Drawing.Imaging.ImageFormat.Png );
stream.Position = 0;
return new Gdk.Pixbuf( stream );
}
}
So, we can take this first function and we can use it in this second snippet which takes an list of images and makes a single pixbuf that contains all of the images. This allows us to take a group of icon files and string them into a single image to be used in a gtk nodeview so that it only requires one column to show a variety of status icons, which is what I’m using it for maybe you can find something more interesting for it to do. Any ways the second snippet I share with you today looks like this.
public Gdk.Pixbuf GenerateIconImage(List<System.Drawing.Image> images)
{
if(images != null && images.Count> 0)
{
Gdk.Pixbuf Composite = ImageToPixbuf(images[0]).Clone() as Gdk.Pixbuf;
images.RemoveAt(0);
foreach (System.Drawing.Image i in images)
{
Composite = CreateComposite(Composite,ImageToPixbuf(i).Clone() as Gdk.Pixbuf);
}
return Composite;
}
else
{
Gdk.Pixbuf Composite = new Gdk.Pixbuf(Gdk.Colorspace.Rgb,true,8,16,16);
Composite.Fill(0);
return Composite;
}
}
public Gdk.Pixbuf CreateComposite(Gdk.Pixbuf p1, Gdk.Pixbuf p2)
{
Gdk.Pixbuf composite = new Gdk.Pixbuf(Gdk.Colorspace.Rgb,true,8,p1.Width+ p2.Width,p1.Height);
p1.Composite(composite,0,0,p1.Width,p1.Height,0,0,1,1,Gdk.InterpType.Hyper,255);
p2.Composite(composite,p1.Width,0,p2.Width,p2.Height,p1.Width,0,1,1,Gdk.InterpType.Hyper,255);
return composite;
}
Tags: c#, gtk, image, mono, pixbuf
Posted in Software | Comments (0)