Problem 134: Prime pair connection

Consider the consecutive primes p_1 = 19 and p_2 = 23.
It can be verified that 1219 is the smallest number such that the last digits are formed by p_1 whilst also being divisible by p_2.

In fact, with the exception of p_1 = 3 and p_2 = 5, for every pair of consecutive primes, p_2 > p_1, there exist values of n for which the last digits
are formed by p_1 and n is divisible by p_2. Let S be the smallest of these values of n.

My Algorithm

I solved this problem twice:
1. a bruteForce approach (finishes in about 140 seconds)
2. a smarter chineseRemainderTheorem solution (finishes in about 0.1 second)

Both call tens(x) which returns the smallest number 10^k that is bigger than x, e.g. tens(456) = 1000.

My brute-force algorithm consists of a simple loop starting at tens(smallPrime) + smallPrime (e.g. 100+19=119 when checking the pair 19,23)
and increments until the division by largePrime has a zero remainder.
Short and simple code but very, very slow ...

The smallest positive solutions S is n mod (p_2 t).
If this is negative, then I have to add the modulo p_2 t once.

Note

Calling tens() every time is overkill: when the current prime exceeds the previous value, then just multiply it by 10.
But I doubt that you observe any noticeable performance gains when optimizing that aspect.

Interactive test

This feature is not available for the current problem.

My code

… was written in C++11 and can be compiled with G++, Clang++, Visual C++. You can download it, too.

#include<iostream>

#include<vector>

// return the smallest 10^k bigger than x

// e.g. tens(456) = 1000 => "a 1 followed by as many 0s as x has digits"

Those links are just an unordered selection of source code I found with a semi-automatic search script on Google/Bing/GitHub/whatever.
You will probably stumble upon better solutions when searching on your own.
Maybe not all linked resources produce the correct result and/or exceed time/memory limits.

Heatmap

Please click on a problem's number to open my solution to that problem:

green

solutions solve the original Project Euler problem and have a perfect score of 100% at Hackerrank, too

yellow

solutions score less than 100% at Hackerrank (but still solve the original problem easily)

gray

problems are already solved but I haven't published my solution yet

blue

solutions are relevant for Project Euler only: there wasn't a Hackerrank version of it (at the time I solved it) or it differed too much

orange

problems are solved but exceed the time limit of one minute or the memory limit of 256 MByte

red

problems are not solved yet but I wrote a simulation to approximate the result or verified at least the given example - usually I sketched a few ideas, too

black

problems are solved but access to the solution is blocked for a few days until the next problem is published

[new]

the flashing problem is the one I solved most recently

I stopped working on Project Euler problems around the time they released 617.

The 310 solved problems (that's level 12) had an average difficulty of 32.6&percnt; at Project Euler and
I scored 13526 points (out of 15700 possible points, top rank was 17 out of &approx;60000 in August 2017)
at Hackerrank's Project Euler+.

My username at Project Euler is stephanbrumme while it's stbrumme at Hackerrank.

Copyright

I hope you enjoy my code and learn something - or give me feedback how I can improve my solutions.All of my solutions can be used for any purpose and I am in no way liable for any damages caused.You can even remove my name and claim it's yours. But then you shall burn in hell.

The problems and most of the problems' images were created by Project Euler.Thanks for all their endless effort !!!

more about me can be found on my homepage,
especially in my coding blog.
some names mentioned on this site may be trademarks of their respective owners.
thanks to the KaTeX team for their great typesetting library !