Số Iccanobif: giống Fibonacci nhưng cộng 2 số trước sau khi đảo ngược chữ số của chúng.
Ví dụ: 0, 1, 1, 2, 3, 5, 8, 13, 39 (13→31, 8→8, 31+8=39), 124, 514, 836...
Thuật toán
- Hàm đảo số: lặp lấy chữ số cuối, xây số mới.
- Khởi tạo first=0, second=1.
- In first, second. Lặp từ 3 đến N: x=rev(first), y=rev(second), next=x+y, in next, cập nhật first=second, second=next.
Ví dụ triển khai (C++)
#include <bits/stdc++.h>
using namespace std;
int reverse_digits(int num) {
int rev = 0;
while (num > 0) { rev = rev*10 + num%10; num /= 10; }
return rev;
}
void icanobifNumbers(int N) {
int first = 0, second = 1;
if (N == 1) { cout << first; return; }
if (N == 2) { cout << first << " " << second; return; }
cout << first << " " << second << " ";
for (int i = 3; i <= N; i++) {
int x = reverse_digits(first);
int y = reverse_digits(second);
cout << x + y << " ";
int temp = second;
second = x + y;
first = temp;
}
}
int main() {
int N = 12;
icanobifNumbers(N);
return 0;
}
Đầu ra
0 1 1 2 3 5 8 13 39 124 514 836