0 . 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 …
The number above is an irrational decimal number created by concatenating all the positive integers in ascending order, and it is called Champernowne's constant .
Let the i th digit of fractional part of Champernowne's constant (base 10) be denoted by a i . Calculate the digital sum of product of all non-zero a i where i is a prime number less than 1000.
Clarification :
The digital sum is sum of all digits of a number. For example, product of a i where i is prime less than 8 is 2 × 3 × 5 × 7 = 2 1 0 . Its digital sum is 2 + 1 + 0 = 3 .
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.
Compared to other programming languages, Python is the most convenient one to solve this problem. This is because it doesn't have an upper bound for an integer and it has strong library to deal with conversions between numbers and conversions.
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 |
|
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 |
|
This gives prod= 1 3 2 9 1 9 9 3 8 1 4 2 9 9 7 9 6 7 8 0 8 4 7 4 1 7 4 7 9 9 7 2 3 0 9 0 4 2 2 8 7 3 4 0 9 5 3 6 2 8 8 3 5 8 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 with digital sum= 2 7 9
The product is obviously a huge number. I printed the powers of each divisor and then used wolfram alpha as C++ refused to print all the digits correctly;
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 |
|
Log in to reply
As agnishom pointed out, range of data types in c++ is very small for solving such problems. That's why I am moving to python. You may use gmp with c++ to improve range a little bit.
Log in to reply
I cannot move to python. I mean, not anytime soon anyway. There are a lot of predefined constructs in python that I think will be really advantageous for a programmer(like functions for permutations). But I have gotten used to C++ and making stuff from scratch. But I will surely learn it after board exam and JEE!
Log in to reply
@Raghav Vaidyanathan – I started learning python 3 days back. Though I still don't know much about different modules
because you do not have a sufficiently large data type in Borland?
Firstly, you can optimize your code to run a lot faster by improving upon your isprime function. Secondly, you can store the number as a string , hence the problem of limit of a datatype vanishes.
Log in to reply
I agree with you that the isprime function can be made better. But can you please tell me exactly how we can store the numer as a string? We have to multiply huge numbers, so how can we do it with a string?
Log in to reply
@Raghav Vaidyanathan – char num[]="1234"; This is how to treat them as strings. Since, in this problem one of the multiplicand is a single digit integer, it is fairly easy to implement the multiplication function. Otherwise also, you can implement it using either normal multiplication or through Gauss's method. Hence, it becomes fairly easy to manage huge numbers.
Log in to reply
@Karttikeya Mangalam – Okay. I get you. Thanks for the suggestions.
Python 3.5.1:
from math import sqrt
c = ""
for i in range(1,1000):
c+=str(i)
def isPrime(n):
if n==1:
return False
else:
for i in range(2,round(sqrt(n))+1):
if n%i==0:
return False
return True
prod = 1
for i in range(1,1000):
if isPrime(i) and int(c[i-1])!=0:
prod*=int(c[i-1])
digitsum = 0
for i in str(prod):
digitsum+=int(i)
print (digitsum)
This problem is slightly more complicated than number 40 on Project Euler. Being rather lazy, I simply adapted code I had already written for use there.
Wrote this in python, got the sieve
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 |
|
Problem Loading...
Note Loading...
Set Loading...
Python 2.7: