Project Euler #3 is :
Find the largest prime factor of a composite number.
My first attempt at this whipped up some typical code that simply brute forced my way to the solution. My code looked like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| static void Problem3() {
long n = 600851475143;
int factor = 2;
int lastFactor = 1;
if(n % 2 == 0) {
lastFactor = 2;
n = n / 2;
while(n % 2 == 0) {
n = n / 2;
}
} else {
lastFactor = 1;
}
factor = 3;
double maxFactor = Math.Sqrt(n);
while(n > 1 && factor <= maxFactor) {
if(n % factor == 0) {
n = n / factor;
lastFactor = factor;
while(n % factor == 0) {
n = n / factor;
}
maxFactor = Math.Sqrt(n);
}
factor += 2;
}
if(n == 1) {
Console.Write(lastFactor.ToString() + " ");
} else {
Console.Write(n.ToString() + " ");
}
} |
Not very elegant, but it works. So I set out to find something a bit prettier and I came across I LINQ solution here. All I did was tack on the .AsParallel() in order to give it a little speed boost. The code looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| static void LinqProblem3() {
long largeNumber = 600851475143;
var allPrimeFactors = from p in Primes.PrimeFactors(largeNumber).AsParallel()
orderby p descending
select p;
foreach(var f in allPrimeFactors){
Console.WriteLine(f);
}
}
public static class Primes {
// Find all prime factors.
public static IEnumerable<int> PrimeFactors(long number) {
// Start by removing the lowest prime (2)
return MorePrimeFactors(number, 2);
}
// This recursive method finds all prime factors.
private static IEnumerable<int> MorePrimeFactors(long number, int factor) {
// Find the next prime factor
while(number % factor != 0)
factor++;
// Return it.
yield return factor;
// look again...
if(number > factor)
// recursively look for this factor again, using Num/factor
// as the new big number
foreach(int factors in MorePrimeFactors(number / factor, factor))
yield return factors;
}
} |
Filed Under (.NET, Development) by Robert Green on 19-06-2009
For some reason it seems that I often need to perform a left join in LINQ. Every time I need to do this I find myself scouring the web one more time in order to remember how a left join works in LINQ. So how does it work? The best example that I’ve found is here. The example looks something like this:
1
2
3
4
5
6
7
8
9
10
| var list = from r in dc.tblRooms
join ui in dc.tblUserInfos
on r.UserName equals ui.UserName into userrooms
where r.CourseID == 1848
from ur in userrooms.DefaultIfEmpty()
select new{
FirstName = (ur.FirstName == null) ? "N/A" : ur.FirstName,
LastName = (ur.LastName == null) ? "N/A" : ur.LastName,
RoomName = r.Name
}; |
I like this example a lot because it is very straight forward. Personally I would like to make the statement a bit more generic. In order to do that all we need to remember is that every Left Join has a left table A and a right table B. All the results from the A will be returned regardless of the join with B. In my example I will call table A the LEFT_TABLE and table B the RIGHT_TABLE.
1
2
3
4
5
6
| var list = from LT in LEFT_TABLE
join RT in RIGHT_TABLE
on LT.key equals RT.KEY into NEW_TABLE
where <CONDITIONS>
from NT in NEW_TABLE.DefaultIfEmpty()
<SELECT_STATEMENT>; |
The only problem that may occur with this left join occurs with the DefaultIfEmpty() operator. A better practice would be to pass in a default value so that we can know what to expect in return.
This morning I came across this jQuery library and it got me to thinking a little bit. All of the web sites that I currently have coded ( I don’t really do the design) tend to use .NET or PHP that is mashed up with some Javascript here and there.
I’m wondering why we tend to still use PHP and .NET except for things that are absolutely on the back end. Why would I use .NET or PHP to manipulate elements in a page when I can just use JavaScript?
Maybe the web community should start developing pages that are JavaScript run and call PHP/.NET WebServices. That puts all the processing for display on the JS and all the data manipulation and retrieval where it should be – on the server.
Any thoughts?
Filed Under (MySQL, Web Development) by Robert Green on 08-06-2009
Whenever I’m doing any WordPress development, I eventually have the need to sync the data between the current dev server, possibly a staging server, and a live server. What’s the easiest way to move a WordPress database between 2 servers?
Let’s start this example by saying that we have 2 servers: Dev and Live. Each server has a mostly identical WordPress installation and each server has it’s own database. In order to sync the initial database from Dev to Live the following steps may be used:
- Use PHPMyAdmin to export the Dev database as a file
- Use PHPMyAdmin to export the Live database as a file (this is a backup, just in case)
- Import the Dev Database into the Live Database using PHPMyAdmin
- Run the following 5 SQL commands in order to update the domain name in your site:
UPDATE wp_multirss SET URL=REPLACE(URL,'Dev_Address','Live_Address');
UPDATE wp_multirss SET Favicon=REPLACE(Favicon,'Dev_Address','Live_Address');
UPDATE wp_posts SET post_content=REPLACE(post_content,'Dev_Address','Live_Address');
UPDATE wp_posts SET guid=REPLACE(guid,'Dev_Address','Live_Address');
UPDATE wp_options SET option_value=REPLACE(option_value,'Dev_Address','Live_Address');
- And now, as long as you’ve written all your relative links correctly inside of your content, you’re finished!
Once you have the initial transfer completed, future transfer are much simpler. Then you only need to complete the above steps by exporting, dropping, and importing the tables that are relevant to the changes that have been made. What tables are those? Honestly, it depends on what you have changed since last time. The best way to figure this out is to go to the Database Description over at WordPress.org to see what you need to move.
Happy migrating!