Palindrome task

So I have a task to find 75 palindromes which squares are also palindromes. I have only the part where it finds palindromes and I need some help/explanation on how to do the squaring of each found palindrome and outputing only 75 of those whose squares are palindromes..
The simplyer code is, the better :D
Solved it thanks to:

shadder
Krulcifer Einfolk

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
#include <iostream>
#include <cstdint>

using namespace std;

int64_t yraPal(int64_t n)
{
    int64_t atv = 0;
    for (int64_t i = n; i > 0; i /= 10)
        atv = atv*10 + i%10;

    return (n==atv);
}

void skaicPal()
{int64_t count=0;
    for (int64_t i = 0;  ; i++)
        if (yraPal(i) && yraPal(i*i))
           {
            cout << i << endl;
            count++;
            if(count==75)
            break;
           }
}


int main()
{
    skaicPal();
    return 0;
}
Last edited on
Here's a way

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
#include<iostream>
using namespace std;

int yraPal(int n)
{
    int atv = 0;
    for (int i = n; i > 0; i /= 10)
        atv = atv*10 + i%10;

    return (n==atv);
}

void skaicPal()
{int count=0;
    for (unsigned long int i = 0;  ; i++)
        if (yraPal(i) && yraPal(i*i))//simple just check if the pallindrome of square is true or not!
           {
            cout << i << endl;
            count++;
            if(count==75)//for 75 int numbers only
            break;
           }
}


int main()
{
    skaicPal();//removed the min max... was a bit constraining
    return 0;//also square of 0 and 1 and 2 and so on is also palindrome
}

//only 21 outputs... 


I'll leave the number repetition part to you :)

PS: I was doubtful if zero was considered a palindrome... googled and found that is is a palindrome....
Last edited on
@shadder
This won't do. You have not even considered 'integer overflow'.

Krulcifer Einfolk
just googled integer overflow.... and edited the code

[edit]
compiling on cpp.sh i got this output


0
1
2
3
11
22
101
111
121
202
212
1001
1111
2002
10001
10101
10201
11011
11111
11211
20002
20102


Why not all? does cpp.sh's console has something wrong with its console or I still didn't got the integer overflow?
Last edited on
@shadder

for (unsigned long i = 0; ; i++)

You just changed int to unsigned long? That is still an 32-bit integer.

By the way :
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
#include<iostream>
using namespace std;

int yraPal(int n)
{
    int atv = 0;
    for (int i = n; i > 0; i /= 10)
        atv = atv*10 + i%10;

    return (n==atv);
}

void skaicPal()
{int count=0;
    for (unsigned long i = 0;  ; i++)
        if (yraPal(i) && yraPal(i*i))//simple just check if the pallindrome of square is true or not!
           {
            cout << count << ". " << i << " (" << i * i << ")" << endl;
            if(++count==75) //for 75 int numbers only
            break;
           }
}


int main()
{
    skaicPal();//removed the min max... was a bit constraining
    return 0;//also square of 0 and 1 and 2 and so on is also palindrome
}


0. 0 (0)
1. 1 (1)
2. 2 (4)
3. 3 (9)
4. 11 (121)
5. 22 (484)
6. 101 (10201)
7. 111 (12321)
8. 121 (14641)
9. 202 (40804)
10. 212 (44944)
11. 1001 (1002001)
12. 1111 (1234321)
13. 2002 (4008004)
14. 10001 (100020001)
15. 10101 (102030201)
16. 10201 (104060401)
17. 11011 (121242121)
18. 11111 (123454321)
19. 11211 (125686521)
20. 20002 (400080004)
21. 20102 (404090404)


And the program stopped. The idea is to use 64-bit integers.

Krulcifer Einfolk
my mistake... I edited it... our code stops at 21... is there any way?
All int(s) should be replaced with int64_t(s). It is just my idea.

Krulcifer Einfolk
Topic archived. No new replies allowed.