+ 1

This challenge is hard really hard

i have to write a program which prints all unlucky numbers to N 1>N>10^13 where unlucky number is one that have 13 in it ex 139 or 513 and sum of it's digits is 13 ex 193 or 940 ex input 1000 output 2 ex input 123456 output 326 i have some code here, but it's slow for bigger numbers: #include <iostream> #include <string> using namespace std; bool sum_13(long n) { long sum = 0; while (n != 0) { sum += n % 10; n /= 10; } if(sum == 13) return 1; else return 0; } bool isThirteen(long n) { if(to_string(n).find("13") != string::npos) return 1; return 0; } int isBadLuckNumber(long n) { return isThirteen(n) && sum_13(n); } int badLuckNumbersToN(long n){ int cnt = 0; for (size_t i = 0 ; i <= n; i++){ if (isBadLuckNumber(i)) cnt++; } return cnt; } long int n; int main(){ cin >> n; cout << badLuckNumbersToN(n); cout << "\n"; return 0; }

18th Nov 2019, 6:56 PM
name
2 Antworten
0
name due you require the solution in C only or can it be in python also?
18th Nov 2019, 7:25 PM
Akash
Akash - avatar
0
c, c++ or python
18th Nov 2019, 7:28 PM
name