The year 2015 marks the end of a remarkable 3 year long period of mathematical significance, the reason being that 2015 is the 3rd (and final) year in a row that is the product of 3 distinct primes.
2 0 1 3 = 3 × 1 1 × 6 1 2 0 1 4 = 2 × 1 9 × 5 3 2 0 1 5 = 5 × 1 3 × 3 1
This isn't the first time this has happened, and it won't be the last! In what year will the next string of 3 consecutive years begin? In other words, if x , x + 1 , x + 2 are all the product of 3 distinct prime numbers, and x > 2 0 1 3 , what is the minimum value of x ?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Another alternative is to use
while loop
or something like
for(int i = 0; ; i++)
Good a solution as any!
I did essentially the same thing in C++! The only difference was that I assumed it would be under 5000 initially, which worked.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Thanks for adding explanations to the steps that you took. It helps others understand the code that you wrote. That's good coding practice!
Nice problem. I had heard about sphenic numbers before but I forgot about it (I've been forgetting much stuff recently). Anyway, after I solved the problem for minimum x , I poked around with my code a bit to see if it could be made stronger since my original code had almost negligible run-time. And so.....
Here's a stronger solution that I made in C++14 (gcc-5.1) that generates the list of such numbers upto a range specified by user, i.e.,
Definition: Call a positive integer k a sphenic master if k , ( k + 1 ) , ( k + 2 ) are all sphenic numbers .
Input: A positive integer t via standard input (#stdin).
Output: List of all sphenic masters ≤ t .
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
Explanation:
Starting with the
main()
block, it takes the input
t
and then dynamically allocates a boolean (
bool
) array of length
(
t
+
1
)
using
new
and sends the base address of the array to
x
which is a
bool
pointer.
x[0]
denotes the first element of array and
x[t]
denotes the last element of array. The
x[i]
's represent the truth value of "
i
is a sphenic number". Initially, all elements of the array are set to
false
. Then, in a loop,
x[i]
runs through
x[2]
to
x[t]
and in the loop,
prime_factors()
assigns
true
or
false
to
x[i]
according as
x[i]
is a sphenic number or not. Then it checks if
x[i],x[i-1],x[i-2]
are all true or not and if they are, that means
(
i
−
2
)
is a sphenic master, so it outputs it along with newline. After the loop terminates, it deallocates the array using
delete
(this is necessary to avoid memory leaks since dynamic memory allocation happens in heap, not in stack).
Now, what happens in
prime_factors()
for each
x[i]
? Simple. I implemented a slight variant of the popular Sieve of Eratosthenes to find all the primes that are less than
i
and are factors of
i
. A counter variable
prime_div_count
initially set at zero for every
x[i]
is incremented everytime a prime factor is found. Also, I created a variable
temp
that holds the value of
i
initially.
temp
is then divided once by each prime factor found. If
i
is sphenic,
t
will be completely divided and will be
1
when all primes of
i
are found and
prime_div_count
will hold the number of prime factors. So, it's tested if the two conditions for
i
to be sphenic is met or not. If it's met, then
true
is returned, else
false
.
Extra info for those interested: I used a trivial number-theoretic fact while implementing
prime_factors()
to reduce run-time as much as I could:If n isn't prime, all the prime factors of n are ≤ 2 n .
You can see the code in action and its output here and are free to fork it and test it yourself by giving an input t . Its run-time is about 1 3 . 8 secs for t = 8 0 0 0 0 . I think that the code can be optimized further (in terms of memory usage and run-time) and extended for higher values of t by using better primality testing algorithms and effectively selecting data types for variables but for now, it's more than enough.
Our relevant part of output :
image
In the output list, it's easy to see that 2 6 6 5 is the number after 2 0 1 3 and hence our answer. □
Chech out my solution !!!!!
Log in to reply
Your code is very brute forced (you stored the entire list of primes till 9000 in an array in your code and used it for primality testing and stuff instead of implementing a primality testing algorithm yourself from scratch). Your code is also not much extendible. See my C++ solution below that uses dynamic memory allocation to implement Sieve of Eratosthenes for primality testing and is a stronger solution because of reasons mentioned there.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
OUTPUT:
1 |
|
Thus, we have to wait for 650 years for this event to occur again!
Great problem! @Garrett Clarke
S i n c e t h e n u m b e r N h a s t o h a v e o n l y t h r e e p r i m e f a c t o r s , 4 c a n n o t b e o n e f a c t o r . H o w e v e r a l l N , N + 1 , N + 2 , m u s t h a v e o n l y t h r e e p r i m e s a s f a c t o r s . I f N ≡ 2 ( m o d 4 ) , t h e t h i r d ( l a s t ) N + 2 ≡ 0 ( m o d 4 ) . ∴ d i v i s i b l e b y 4 . I f N ≡ 3 ( m o d 4 ) , t h e s e c o n d ( m i d d l e ) N + 1 ≡ 0 ( m o d 4 ) . ∴ d i v i s i b l e b y 4 . S o w e n e e d c h e c k o n l y N ≡ 1 ( m o d 4 ) . I u s e d f a c t o r i n g c a l c u l a t o r . S o I s t a r t e d w i t h N = 2 0 1 7 . ( A ) C h e c k e d N i f i t h a d o n l y t h r e e p r i m e s a s f a c t o r s . I f Y E S t h e n c h e c k e d f o r N + 1 , o t h e r w i s e s k i p t o N O . I f Y E S t h e n c h e c k e d f o r N + 2 , o t h e r w i s e s k i p t o N O . I F Y E S , t h i s i s t h e s o l u t i o n . I f N O , l e t N = N + 4 , a n d c y c l e a g a i n f r o m ( A ) . W i t h e v e r y N O I a d d e d 4 t i l l I h a d Y E S , Y E S , Y E S a t N + 2 = 2 6 6 7 . S o N = 2 6 6 5 .
Here is my solution in python 3.4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
Problem Loading...
Note Loading...
Set Loading...
I made a assumption that the number will be below 9999
I used C code to crack this question as i was not aware about Sphenic number
prime is the list of prime number.
numc() function return 1 or 0 based on the condition if the argument number N is having 3 distinct prime number then it will return 1 else 0.
memset() is a function which will initialize all element of array to 0 given in function.
The ans is 2665!