Categories
Coding Project Euler R

Project Euler Problem #19

Problem 19 on the Project Euler website asks the user, given some initial information:

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

The obvious (but longer) way is to calculate the sum of the days between 1901 and 2000, given the number of days in each month, and a helper function to determine whether a year is a leap year or not:

is.leap <- function(year) {
        return (year %% 4 == 0 || (year %% 100 == 0 && year && 4 == 0))
}

# Problem 19
problem19 <- function() {
        daycount <- 1
        daylist <- list()
        i <- 1
        for (year in 1900:2000) {
                months <- c(0,31,28,31,30,31,30,31,31,30,31,30,31)
                if (is.leap(year)) {
                        months[3] <- 29
                        }
                        days <- daycount + cumsum(months)
                        daycount <- days[length(days)]
                        daylist[[i]] <- (days[(length(days))])
                        i <- i + 1
                }
                sum(unlist(lapply(daylist[1], function(x) {sum(x %% 7==1)} )))
}

However, with the aid of R’s chron library, there is a much easier way:

# Problem 19, method 2
library(chron)
sum(weekdays(seq.dates(”01/01/1901″, â€œ12/31/2000″, by=”months”))==”Sun”)

Categories
Coding

Webank Event at NESTA

Just attended the webank event at NESTA tonight, which was well attended with a capacity crowd of finance industry entrepeneurs, lawyers, VCs and bloggers. Three interesting companies presented at the event, each with their own angle on p2p finance:

Zopa presented a brief introduction to their model and gave some interesting statistics about their typical user base. Zopa (UK at least) seems to be profiting handsomely from the current credit market difficulties, although it seems that they may not be quite yet in the black. Their US operation seems to be less of a p2p operation. Average returns apparently around 9%, with a default rate of around 0.2%.

Kubera Money presented on their vision of a p2p lending platform based on the chit fund, or ROSCA model. Not too much detail regarding the actual implementation of how the schemes will work in the distributed world, but sounds like they have the FSA regulatory hurdles cleared, so hopefully will progress fast.

Midpoint Transfer was the last startup to present, and their business model revolves around disintermediating FX dealers by offering FX rates at the midpoint (no spread) with all trades offset via a linked network of midpoint-controlled intermediary accounts. Trades are guaranteed to be matched same day and midpoint takes on the credit risk. There is a flat $30 fee per transaction. Having worked in FX for a couple of years, I am sceptical about a couple of things, mainly around their guaranteed matching (which necessitates them having to hedge any unmatched exposure themselves, which will be a common occurrence unless they have thousands of users), and the fact that the midpoint is not set until the trade is actually matched, which could entail a very unfavourable price for a customer if a trade is not matched until end-of-day during a period of high volatility. If this is the case, there is a real risk that customers in this case would have been much better off by just dealing through a broker.

Afterwards, there was an entertaining verbal sparring match between the Zopa CEO and James Gardner, the head of innovation at a large retail bank, who both argued their views eloquently. Met some interesting people at the event – there are a lot of innovative ideas coming down the line in p2p finance.

Categories
Coding

Debugging DLL Loading Issues in Windows

Attempting to debug dynlib loading issues in Windows can be tricky – on Linux you can use strace to monitor dynamic linking and see what libraries are being loaded as the application runs. Having just had some issues with dynlib loading issues in an R extension I wrote, I was keen to find a good way to debugging the issues as they occurred. Thankfully, there are some good utilities out there for debugging this kind of thing:

Dependency Walker is an old SDK tool that is still invaluable. Drag a .DLL or .EXE into the application window and it will show you the dependencies, what type of loading mechanism they use (e.g. eager or delay load), and also what exported functions from the dependent DLLs are actually used by the .DLL or .EXE you have selected.

Here is a screenshot of depends.exe operating on my R extension DLL. Note that MSCVR80.DLL and R.DLL are both highlighted as missing – this is extremely useful for figuring out dependent DLL load issues.

Dependency Walker
Dependency Walker

The SysInternals Process Monitor is an incredibly useful low-level Swiss Army Knife utility that can be used, among other things, to monitor dynamic library loading activity as it occurs, using the file activity view. Here is a view of Process Monitor monitoring R as it loads my extension DLL. The subsequent dependent DLL loading activity is highlighted.

Process Explorer
Process Explorer