The second problem is pretty easy using LINQ (like the first was).
We'll need a few helper methods - the provide some support for the use of the BigInteger class.
private static BigInteger SumOfEvenTermsUnderFourMillion()
{
return Numbers.Fibonacci
.Skip(2)
.TakeWhile(x => x < 4000000)
.Where(x => x.IsEven)
.Sum()
}
In order to get the Fibonacci numbers, I created an unbound, on-demand iterator. It will keep generating Fibonacci numbers until no more are requested. In the case of this solution, as long as the numbers are less than 4,000,000.
public static class Numbers
{
public static IEnumerable<BigInteger> Fibonacci
{
get
{
yield return BigInteger.Zero;
yield return BigInteger.One;
BigInteger previous = BigInteger.Zero;
BigInteger current = BigInteger.One;
while (true)
{
BigInteger next = previous + current;
previous = current;
current = next;
yield return next;
}
}
}
}
The first problem is really easy to solve, especially with LINQ extension methods.
For starters, I decided to use the BigInteger class from System.Numerics. And since the each problem build on previous problems, I should wind up with a lot of reusable code.
private static BigInteger SumOfNaturalsLessThan1000()
{
return BigInteger.One.To(1000)
.Where(IsDivisibleByThreeOrFive)
.Sum();
}
private static bool IsDivisibleByThreeOrFive(BigInteger number)
{
return number.IsEvenlyDivisible(3) || number.IsEvenlyDivisible(5);
}
So already we have three methods that need a definition: To, IsEvenlyDivisible, and Sum.
The To method will be a simple iterator extension method. The IsEvenlyDivisible method is another extension method that can be easily implemented using modulus or with the BigInteger.Remainder method. And Sum extension method will use LINQ aggregation to do the summation.
public static IEnumerable<BigInteger> To(
this BigInteger start,
BigInteger end)
{
for (BigInteger x = start; x <= end; x++)
{
yield return x;
}
yield break;
}
public static bool IsEvenlyDivisible(
this BigInteger dividend,
BigInteger divisor)
{
BigInteger remainder = BigInteger.Remainder(dividend, divisor);
return remainder.IsZero;
}
public static BigInteger Sum(this IEnumerable<BigInteger> source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
return source.Aggregate(BigInteger.Zero, BigInteger.Add);
}
It's been a long time coming, but I've rewritten the CMS behind WS in C#. I've been really lazy about doing anything with this site, but I'm hoping to change that. I've even given it a new look and feel.
On the plus side, the decision to back this with XML rather than a database served me well in the migration. The level of customization of XML serialization made it easy to retain the data (without changes) and concentrate on creating the .NET classes.
There will still need to be some legacy Classic ASP pages, but they will all serve as redirects to the corresponding .NET pages.
If nothing else, the advent of master pages makes using ASP.NET really easy to customize and keeps page-specific content relegated to the page itself.
Further, the use of on-demand iterator blocks makes it really easy to retrieve content while only generating objects I need.
This is really just a random assortment of ideas in my head while writing the Twitlbl API.
- The biggest concern is ease of modification. I know this version of the API will be wrong and incomplete, but I don't want to alienate anyone who does decide to use it when I try to fix it. To that end, the version of the API is included in the service endpoints. Future versions of the API will make use of the same structure, without breaking the existing implementors.
- To a degree, the implementation of the external API is validation of the internal API. It was fairly easy to create the API and make it work.
- I spent more time writing the documentation than I did writing the implementation of the API. I'm not happy with the documentation, but I feel it is a good start.
- Dogfooding your own external API is invaluable. My initial design for it was a lot different. After attempting to work with it, a lot has been changed to make it easier to work with.
At this point, I feel have a decent first pass. I'm hoping that someone other than myself will try playing with it I would love to get some feedback on it, and the service in general.
As for planning future expansion, I'm currently evaluating oEmbed, oData, and JSON support to broaden adoption and use. I'm hoping in the coming days to post a development roadmap of sorts as well an architecture writeup.
A while ago I was playing a lot of two-player Settlers of Catan. I apparently wrote down the house rules we were using. The idea behind them is to make things fun while attempting to preserve game balance.
Starting Hand
Give a player resources cards for both settlements laid during the setup phase of the game, rather than the last settlement laid. This will make the beginning of the game progress a bit faster. Since resources are only being accumulated by the limited rolls of two players, the game tends to drag.
Robber
In a two-player game, you end up stealing from the same person again and again. An alternative is whenever a 7 is rolled, to re-roll the dice and place the robber in a hex corresponding to the number rolled. If the number rolled has multiple playable hexs, its the players choice on where to place the robber. If a 7 is rolled on this re-roll, the robber returns to the desert.
After the robber has been moved, if an opponent has a settlement or city on that hex, the player may steal a card.
This rule supercedes the requirement that a robber is moved into a position that affects a player, and cannot be moved to the desert (if the above option to move to the desert is used).
"Phantom" Players
This alternate rule is best played in the absence of other house rules. The purpose of the phantom players is to provide the limitations that other plays would impose (resources, robber, board layout) while providing for the potential resource gain of having those additional players.
Initial Setup
Each player rolls to determine who places a settlement first, herein known as the "First" and "Second" player. The second player first places a settlement of a phantom player on the board. The the First player places their first settlement, followed by the second player of their own settlements, followed by a settlement of the other phantom player by the first player. This order is reversed as per the rules, with each player placing a settlement of the phantom player they did not place in the first portion. Basically, each player should place both of their own settlements and roads, as well as one of each of the phantom's settlements and roads.
For example, Player 1 is playing red and Player 2 is playing blue, with phantoms playing orange and white. Player one won the initial die roll. The order would be: Player 2/Orange, Player 1/Blue, Player 2/Red, Player 1/White, Player 2/White, Player 2/Red, Player 1/Blue, Player 1/Orange.
Game Play
Between each real player's turn, a phantom player should be rolled. Resource cards distributed as appropriate. If a 7 is rolled by a phantom, the dice are rolled to determine where a robber moves - if a 7 comes up on the placement roll, the robber returns to the desert; if the robber is in play and the same number is rolled, the robber moves to the other tile (if available) or back to the desert (if there is no second tile).
Other than die rolls, the phantoms do not collect resource cards or participate in any way.
Articles