CSharpFeeds - All your C# feeds in one place.

Sponsors

Sunday, September 06, 2009

Use LINQ Aggregate to Multiply a Series of Digits

by ssmith via Blog on 9/6/2009 4:39:00 AM

The LINQ Aggregate() extension method uses a Func<int, int, int> to operate on items in a series.  If you want to use it, for example, to return the product of each value with its successor, you can do something like this:

Func<int, int, int> producter = (one, two) => one * two;
var result = subString.ToCharArray().ToDigits().Aggregate(producter);

Of course, you don’t need the intermediate value.  You can simply use a lambda directly for the Aggregate()’s parameter:

//Func<int, int, int> producter = (one, two) => one * two;
var result = subString.ToCharArray().ToDigits().Aggregate((p1,p2) => p1 * p2);

With a loop to keep track of the largest result returned for a substring of length 5, you can easily use this technique to solve Euler 8.

email it!bookmark it!digg it!

Original Post: Use LINQ Aggregate to Multiply a Series of Digits

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us