Project Euler, Visual Studio and playing with Python

I got Visual Studio 2017 through Microsoft’s Dreamspark promotion and thought I’d give it a try. I haven’t done a lot of development under Windows because its only on my laptop and my Linux desktop has 12 Gb RAM.

Common “Workloads”, supporting languages are available – I installed Python and R.

Visual Studio Workloads
Visual Studio Workloads

The IDE is comfortable to use and fairly intuitive. It’s very customizable, I set mine with a dark theme. Code completion, Intellisense as Microsoft calls it, is helpful with short descriptions of methods and their parameters as you type.

Visual Studio 2017
Visual Studio 2017 dark theme

CVS is integrated, maybe not surprising as Microsoft recently aquired GitHub.  The notation for keyboard shortcuts struck me as unituitive though – “Ctrl+K, Ctrl+D” means to hold the control key, press K then D.

Shortcut notation
Unituitive maybe?

On to Project Euler. I haven’t used Python since M269 (Algorithms Data Structures and Computability). This computer science course was developed alongside MIT, where I understand Python is ubiquitous. Python is simpler than the Java I’ve been using so much in the last few years – its terseness offers a simple elegance.

Project Euler problem 20 is to find 100! (the factorial of 100) and sum all the digits. My solution was to find the factorial, store it as a string then sum the digits by iterating across the string.

import math

factorial = str(math.factorial(100))
sumOfDigits = 0
for digit in factorial:
    sumOfDigits = sumOfDigits + int(digit) 
Project Euler’s way of bringing you down to Earth.

R is completely new to me. I read an excellent introduction and opened a new R project. Finding it similar to Python I approached the problem in the same way. The first thing I found is R has no factorial method. So I opted to use recursion – a factorial is after all the natural numbers up to some limit. I do like the way variable names can contain a period – very readable.

Next I found that R uses a datatype called a vector, so I’d could store the answer in a form I could iterate using the unlist function.

factorial <- function(natural.number) {
    if (natural.number <= 1) { return(1) }
    else { return(natural.number * factorial(natural.number - 1)) }
sum.digits <- function(number) {
    return(sum(as.numeric(unlist(strsplit(as.character(number), split = "")))))

So this works fine for small factorials, however with 100:

Warning message:
In sum.digits(factorial(100)) : NAs introduced by coercion

NA is a “not available” – a missing value. I realised that large numbers are in scientific notation and this is being encoded in the string, so it can’t add the “e”. R has the GNU Multiple Precision Aritmetic Library available, which needs to be added to Visual Studio using R Package Manager (Ctrl+7). This can not only find factorials but has a useful method, factorialZ, that computes factorials as an arbitrarily large integer type known as bigz.

sum.digits <- function(number) {
return(sum(as.numeric(unlist(strsplit(as.character(number), split = "")))))

Got there in the end. Added bonus: learned how to install an R package in Visual Studio; found a useful package; learned a bit about precision and big numbers; and grasped the basics of a new language.

Leave a Reply

Your email address will not be published.