Dont know what type to use for this lambda parameter

I have a function max_palprime(n) that is supposed to find the highest palindromic prime less or equal to n. This function also works together with other two: prime_sieve(m) that generates a sieves of prime numbers up to m, and pal_test(n) that tests if a given number is a palindrome. To simplify the code I already had I was suggested to use the following function for max_palprime(n):
1
2
3
4
5
6
7
8
9
10
long long inpal::max_palprime(long long n)
{
    auto primes = prime_sieve(n);

    auto it = std::find_if(primes.rbegin(), primes.rend(), [&](auto it) {
        auto num = primes.size() - std::distance(primes.rbegin(), it)
        return *it && pal_test(num);
    });
    return primes.size() - std::distance(primes.rbegin(), it);
}

but it currently doesnt work because the compiler is telling me that I cant use auto as a lambda parameter. What type do I use in this situation or is there a better way to implement the code?


Here are the other two functions if needed:
prime_sieve(m):
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
std::vector<bool> inpal::prime_sieve(long long m)
{
    std::vector<bool> p_test(m+1, false);
    
    //defines square root of m
    unsigned long long root = ceil(sqrt(m));
    
    //sieve axioms
    for(unsigned long long x=1; x<=root; x++)
    {
        for(long long y=1; y<=root; y++)
        {
            long long i=(4*x*x)+(y*y);
            if (i<=m && (i%12==1 || i%12==5))
            {
                p_test[i].flip();
            }
            i=(3*x*x)+(y*y);
            if(i<=m && i%12==7)
            {
                p_test[i].flip();
            }
            i=(3*x*x)-(y*y);
            if(x>y && i<=m && i%12==11)
            {
                p_test[i].flip();
            }
        }
    }
    
    //marks 2,3,5 and 7 as prime numbers
    p_test[2]=p_test[3]=p_test[5]=p_test[7]=true;
    
    //marks all multiples of primes as non primes
    for(long long r=5; r<=root; r++)
    {
        if((p_test[r]))
        {
            for(long long j=r*r; j<=m; j+=r*r)
            {
                p_test[j]=false;
            }
        }
    }
    
    return p_test;
}


pal_test(n):
1
2
3
4
5
6
7
8
9
10
11
12
13
bool inpal::pal_test(long long n)
{
    //converts n to a string
    std::string rev = std::to_string(n);
    
    //checks if the reverse of rev is equal to rev
    if(std::equal(rev.begin(), rev.begin()+rev.size()/2, rev.rbegin()))
    {
        return true;
    }
    
    return false;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
std::size_t inpal::max_palprime( std::size_t n )
{
    // assert( n > 1 ) ;

    const auto primes = prime_sieve(n); 
    // assert( primes.size() == n+1 ) ;

    for( std::size_t i = n ; i >= 2 ; --i ) if( primes[i] && pal_test(i) ) return i ;

    return 2 ;
}
I'm not sure OP really wants help. (He keeps posting the same problem in new threads, ignoring all the advice given him.)

http://www.cplusplus.com/forum/beginner/193151/
http://www.cplusplus.com/forum/beginner/193543/
http://www.cplusplus.com/forum/beginner/193553/
Would you advice the use of std::size_t in the other functions instead of long long. Or was this just an exception?
it depends

size_t is unsigned i.e. it wont represent negative numbers, long long will be able to represent negative numbers too
> Would you advice the use of std::size_t in the other functions

Definitely for
1
2
3
4
5
std::vector<bool> inpal::prime_sieve( /*long long*/ std:size_t m )
{
    std::vector<bool> p_test(m+1, false);
    // ...
}


Programs that use other types, such as unsigned int, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX or if it relies on 32-bit modular arithmetic.

When indexing C++ containers, such as std::string, std::vector, etc, the appropriate type is the member typedef size_type provided by such containers. It is usually defined as a synonym for std::size_t.
http://en.cppreference.com/w/cpp/types/size_t
Topic archived. No new replies allowed.