-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.cpp
More file actions
52 lines (42 loc) · 1.32 KB
/
Copy path36.cpp
File metadata and controls
52 lines (42 loc) · 1.32 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
#include <bits/stdc++.h>
using namespace std;
// Cek palindrom basis 10 (Matematis murni)
bool is_palindrome_base10(int n) {
int original = n;
int reversed = 0;
while (n > 0) {
reversed = reversed * 10 + (n % 10);
n /= 10;
}
return original == reversed;
}
// Cek palindrom basis 2 (Bitwise / Matematis murni)
bool is_palindrome_base2(int n) {
int original = n;
int reversed = 0;
while (n > 0) {
reversed = (reversed << 1) | (n & 1);
n >>= 1;
}
return original == reversed;
}
int main() {
// 1. Catat waktu tepat sebelum loop dimulai
auto start = chrono::high_resolution_clock::now();
int res = 0;
// Proses loop utama
for(int i = 1; i < 1000000; i += 2) {
if(is_palindrome_base10(i) && is_palindrome_base2(i)) {
res += i;
}
}
// 2. Catat waktu tepat setelah loop selesai
auto stop = chrono::high_resolution_clock::now();
// 3. Hitung selisih waktunya (konversi ke mikrodetik dulu biar presisi)
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
// Tampilkan hasil pencarian
cout << "Hasil: " << res << endl;
// Tampilkan durasi eksekusi dalam milidetik (ms)
cout << "Waktu Eksekusi Murni: " << duration.count() / 1000.0 << " ms" << endl;
return 0;
}