Project Euler 179 - Consecutive positive divisors

Find the number of integers 1 < n < 10^7, for which n and n + 1 have the same number of positive divisors.

For example, 14 has the positive divisors 1, 2, 7, 14 while 15 has 1, 3, 5, 15.

Official link: https://projecteuler.net/problem=179

Thought Process

First, generate an array (called values) that is 1 + 10^7 long which looks like the following: values = [0,1,1,1,1, .... ,1,1,1]

Now create 2 nested loops, for variables x and y.

1) x go from 2 to 10^7 / 2

2) y go from 1 to 10^7 / x

loop through x then y, then values[x*y] += 1


This is because the index of values[x*y] is the number x*y, so clearly x is a divisor of x*y

For example when x = 2, y = 1, x*y = 2, so the number 2 has the divisors 2, and values[2] = 1, so when we add one

values[2] = 2, which implies that 2 has 2 divisors

Interactive Code

Enter a number (yourinput)

Code will output the anwser from 1 to yourinput