-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.cpp
More file actions
83 lines (70 loc) · 1.64 KB
/
4.cpp
File metadata and controls
83 lines (70 loc) · 1.64 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
*/
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
bool is_palindrome(unsigned long long);
bool is_prime(unsigned long long);
int main()
{
vector<unsigned long long> p;
for (unsigned long long i = 10000; i < 998002; i++)
if(is_palindrome(i))
if(!is_prime(i))
p.push_back(i);
cout << "Maximum palindrome in the scope: " << p.back() << endl;
cout << "Size = " << p.size() << endl;
bool flag = false;
for (vector<unsigned long long>::iterator it = p.end() ; it != p.begin(); --it)
{
for (int i = 100; i < 1000; ++i)
{
if (*it%i == 0 && *it != 0)
{
cout << "Result = " << *it << endl;
cout << "Factor = " << i << " and " << *it/i << endl;
if (*it/i < 1000)
{
flag = true;
break;
}
else
continue;
}
}
if (flag) break;
}
}
bool is_palindrome(unsigned long long obverse)
{
unsigned long long reverse = 0;
unsigned long long number = obverse;
while(number)
{
reverse = reverse * 10 + number % 10;
number = number / 10;
//cout << "reverse = " << reverse << endl;
}
if (obverse == reverse)
return true;
else
return false;
}
bool is_prime(unsigned long long n)
{
unsigned long long i,sq,count=0;
if(n==1 || n==2)
return true;
if(n%2==0)
return false;
sq=sqrt(n);
for(i=2;i<=sq;i++)
{
if(n%i==0)
return false;
}
return true;
}