We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Project Euler #30: Digit Nth powers
Project Euler #30: Digit Nth powers
Contest ends in
+ 0 comments One test case failing.
n=int(input().strip()) sum_=0 for i in range(10**(n-1),10**n): s=0 t=i while t>0: r=t%10 s+=r**len(str(i)) t=t//10 if s==i: sum_+=i print(sum_)
+ 1 comment One test case failing. Can you find what's wrong with this code?
lookup={} for i in range(100,10**6): s=0 t=i while t>0: r=t%10 s+=r**len(str(i)) t=t//10 if s==i: lookup[len(str(i))]=i if len(str(i)) not in lookup else lookup[len(str(i))]+i n=int(input().strip()) print(lookup[n])
+ 0 comments Java code
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); long sum = 0; for (long i = 2; i < 1000000; i++) { long temp = i; long sum1 = 0; while (temp > 0) { int a = (int)(temp % 10); sum1 += (long)Math.pow(a, n); temp = temp / 10; } if (sum1 == i) { sum += sum1; } } System.out.println(sum); scanner.close(); } }
+ 0 comments 100/- points python3
def sum_calc(no,power): '''It calculates sum of digits of no raised to power''' sum=0 for i in str(no): sum+=int(i)**power return sum li=[]#It stores the max number we have to check till for a power for n in range(3,7): x=2 while True: if 9**n*x<10**(x-1): #The max sum with x digits should'nt be less than the least number with x digits break x+=1 li.append((n,9**n*(x-1))) answers={} for item in li: sums=0 #it calculates the required sum for no in range(2,item[1]+1): if sum_calc(no,item[0])==no: sums+=no answers[item[0]]=sums n=int(input()) print(answers[n])
+ 0 comments code in c
int main() {
int n; scanf("%d",&n); unsigned long long int sum=0; for (long i =2; i<1000000; i++) { long temp = i; unsigned long long int sum1 = 0; while (temp>0) { int a = temp%10; sum1 +=(unsigned long long int) pow(a, n); temp = temp/10; } if (sum1 == i) { sum += sum1; } } printf("%lld",sum); return 0;
}
Load more conversations
Sort 47 Discussions, By:
Please Login in order to post a comment