-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprob4.c
51 lines (44 loc) · 947 Bytes
/
prob4.c
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
/* file: prob4.c
author: David De Potter
description: extra, problem 4, binary palindromes
*/
#include <stdio.h>
#include <stdlib.h>
int isPalindrome(int n) {
// checks if n is a binary palindrome with
// odd number of 1s
int rev = 0, orig = n, sum = 0;
while (orig) {
rev = (rev << 1) | (orig & 1);
orig >>= 1;
sum += rev & 1;
}
return rev == n && sum % 2;
}
void printBinary(int n) {
// prints the binary representation of n
while (n) {
printf("%d", n & 1);
n >>= 1;
}
}
void generatePalindromes(int a, int b) {
// generates all binary palindromes in [a, b]
// having odd number of 1s
int found = 0;
for (int i = a; i <= b; ++i)
if (isPalindrome(i)) {
found = 1;
printf("%d: ", i);
printBinary(i);
printf("\n");
}
if (!found)
printf("0\n");
}
int main() {
int a, b;
(void)! scanf("%d %d", &a, &b);
generatePalindromes(a, b);
return 0;
}