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.
I made this code in C. It works with the sample case, but I fail in all the test cases due to timeout. Why is that?
#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#include<assert.h>#include<inttypes.h>intcheckPrime(uint64_ts){/*gets a number and return 1 for prime or 0 otherwise*/uint64_ti;if(s<=1)return0;if(s==2)return1;if(s%2==0&&s>2)return0;elsefor(i=3;i<s/2;i+=2){if(s%i==0)return0;}return1;}intsumSqrDigits(uint64_ta){/* sum the square of all digits of a number*/uint64_ts=0;if(a<=10)return0;while(a>0){s+=(a%10)*(a%10);a=a/10;}returncheckPrime(s);/* checks if the sum is a prime number*/}intsumDigits(uint64_ta){//sum the digits of a numberuint64_ts=0;if(a<=10)return0;elsewhile(a>0){s+=a%10;a=a/10;}returncheckPrime(s);/* checks if the sum is a prime number*/}intluckyNumbers(uint64_ta,uint64_tb){/*iterates through all numbers between a and b (included)*/uint64_tans=0,i;for(i=a;i<=b;i++){if((sumDigits(i)==1)&&(sumSqrDigits(i)==1)){// checks if both results are primeans++;//printf("%"PRIu64"\n", ans);}}returnans;}intmain(){uint64_ta,b,i,cases;scanf("%"SCNu64,&cases);uint64_tans[cases];for(i=0;i<cases;i++){scanf("%"SCNu64"%"SCNu64,&a,&b);ans[i]=luckyNumbers(a,b);}for(i=0;i<cases;i++){printf("%"PRIu64"\n",ans[i]);}return0;}
Lucky Numbers
You are viewing a single comment's thread. Return to all comments →
I made this code in C. It works with the sample case, but I fail in all the test cases due to timeout. Why is that?