Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctime>
- #include <iostream>
- #include <algorithm>
- typedef unsigned long long main_type;
- main_type evaluated=0,
- is_palindrome_called=0;
- bool is_palindrome(main_type n){
- is_palindrome_called++;
- if (!n)
- return 1;
- char digits[100]={0};
- size_t s=0;
- while (n){
- digits[s++]=n%10;
- n/=10;
- }
- if (s<3)
- return 1;
- //reusing n:
- n=s/2+s%2;
- for (size_t a=0;a<n;a++)
- if (digits[a]!=digits[s-a-1])
- return 0;
- return 1;
- }
- struct result_t{
- main_type candidate,x,y;
- };
- #define N 999
- result_t get(){
- bool found=0;
- result_t result;
- for (main_type x=N;x>99;x--){
- main_type end=99;
- if (found)
- end=result.candidate/x;
- if (end>=N)
- break;
- for (main_type y=N;y>end && y>=x;y--){
- evaluated++;
- if (found && x*y<=result.candidate)
- continue;
- if (!is_palindrome(x*y))
- continue;
- if (!found || x*y>result.candidate){
- result.candidate=x*y;
- result.x=x;
- result.y=y;
- found=1;
- break;
- }
- }
- }
- return result;
- }
- int main(){
- result_t r;
- clock_t t0=clock();
- const unsigned repeats=1000;
- main_type max_evaluated=0,
- max_is_palindrome_called=0;
- for (unsigned a=0;a<repeats;a++){
- r=get();
- max_evaluated=std::max(max_evaluated,evaluated);
- evaluated=0;
- max_is_palindrome_called=std::max(max_is_palindrome_called,is_palindrome_called);
- is_palindrome_called=0;
- }
- clock_t t1=clock();
- std::cout <<r.candidate<<std::endl;
- std::cout <<max_evaluated<<std::endl;
- std::cout <<max_is_palindrome_called<<std::endl;
- std::cout <<double(t1-t0)/repeats<<std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment