Thread: Getting a different answer to the one expected/published

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    Getting a different answer to the one expected/published

    Truncatable primes - The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

    The following is my effort to sole this. the answer i get is 573 which makes sense until you consider there is only supposed to be 11 and the example given is bigger than the 11 i found

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define Countto 1000000
    
    int FindMod( int *, int );
    void LoadPrimes( int *Primes );
    int CheckPrime( int *, int );
    int Truncatable( int *, int, int, int );
    
    int main()
    {
        int NumSieve[Countto] = { 0 };
        int Sum = 0, Counter = 0;
    
        LoadPrimes( NumSieve );
    
        for (int i = 11; i < Countto; i += 2)
        {
            int Mod = 10, Limit = FindMod( &Mod, i );
    
            if ( CheckPrime( NumSieve, i % Mod) )
                if ( Truncatable(NumSieve, Mod, Limit, i ) )
                {
                    printf("%d\n", i);
                    Sum += i;
                    Counter++;
                }
            if ( Counter == 11 )
                break;
        }
    
        printf("Sum of the 11 truncatable primes is: %d\n", Sum);
        return 0;
    }
    
    int FindMod( int *Mod, int Num )
    {
        int Counter = 0;
    
        while ( Num % *Mod < Num )
        {
            *Mod *= 10;
            Counter++;
        }
    
        return Counter;
    }
    
    void LoadPrimes( int *Primes )
    {
        for ( int i = 4; i < Countto; i += 2 )
            Primes[i] = 1;
    
        for ( int i = 3; i < 1000; i += 2 )
        {
            if ( Primes[i] == 0 )
                for ( int j = i * 2; j < Countto; j += i)
                    Primes[j] = 1;
        }
    }
    
    int CheckPrime( int *Primes, int Index )
    {
        return Primes[Index] == 0; //Index is prime
    }
    
    int Truncatable( int *NumSieve, int Mod, int Limit, int i )
    {
    
        int TruncNum[Limit];
    
        for ( int j = 0; j < Limit; j++ )
        {
            Mod /= 10;
            if ( !CheckPrime( NumSieve, i % Mod ) )
                return 0;
    
            TruncNum[j] = i % Mod;
        }
    
        for ( int j = Limit - 1; j >= 0; j-- )
        {
            if ( !CheckPrime( NumSieve, ( i - TruncNum[j] ) / Mod  ) )
                return 0;
    
            Mod *= 10;
        }
    
    
        return 1;
    }
    where am i going wrong

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by cooper1200 View Post
    Truncatable primes - The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

    The following is my effort to sole this. the answer i get is 573 which makes sense until you consider there is only supposed to be 11 and the example given is bigger than the 11 i found

    where am i going wrong
    191 * 3 = 573

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    ummmm 573 is the sum of the truncatable primes i found

    the truncatable primes are
    Code:
    11
    13
    17
    23
    31
    37
    53
    71
    73
    113
    131
    if you add them up you get 573
    Last edited by cooper1200; 06-03-2023 at 01:22 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by cooper1200 View Post
    ummmm 573 is the sum of the truncatable primes i found

    the truncatable primes are
    Code:
    11
    13
    17
    23
    31
    37
    53
    71
    73
    113
    131
    if you add them up you get 573
    1 is defined as not prime!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    so is 3 and 7 but they count those on 3795

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by cooper1200 View Post
    so is 3 and 7 but they count those on 3795
    Wow, you are truly ignorant about math.

    Goodbye.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by stahta01 View Post
    Wow, you are truly ignorant about math.

    Goodbye.

    Tim S.
    Prime number - Wikipedia
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by stahta01 View Post
    Wow, you are truly ignorant about math.

    Goodbye.

    Tim S.
    nothing to do with mathS i can speak and understand plain English

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by cooper1200 View Post
    nothing to do with mathS i can speak and understand plain English
    You do understand that 2 is the smallest prime number, correct?

    If you do not understand that then this problem is not one you can solve till you understand more about prime numbers.

    Therefore all the numbers that start and end with "1" do not count as valid for this problem. I have no idea if the ones with "1" in the middle count as valid.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int isprime(int n) {
        if (n < 2) return 0;
        if (n % 2 == 0) return n == 2;
        for (int d = 3; d * d <= n; d += 2)
            if (n % d == 0) return 0;
        return 1;
    }
     
    int is_truncatable_prime(int n) {
        if (!isprime(n)) return 0;
        for (int m = n / 10; m; m /= 10)
            if (!isprime(m)) return 0;
        char s[20];
        int len = sprintf(s, "%d", n);
        for (int i = 1; i < len; ++i)
            if (!isprime(atoi(s + i))) return 0;
        return 1;
    }
     
    int main () {
        int sum = 0;
        for (int p = 11, i = 0; i < 11; p += 2)
            if (is_truncatable_prime(p)) {
                printf("%6d\n", p);
                sum += p;
                ++i;
            }
        printf("======\n%6d\n", sum);
        return 0;
    }
    /*
    Output:
        23
        37
        53
        73
       313
       317
       373
       797
      3137
      3797
    739397
    ======
    748317
    */
    A little inaccuracy saves tons of explanation. - H.H. Munro

  11. #11
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    ok am i understanding this correctly? in the function is_truncatable_prime you test if the number passed is not prime ie if a number is prime the if statement fails and the function continues. However, function is_prime tests weather the number is even or not and if it is returns 2. Does this not cause the !is_prime to fail and therefore allow even numbers through.

  12. #12
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    is_prime never returns 2. It returns the value of n == 2, i.e., it returns 1 if n is 2 and 0 if it isn't.
    Basically, if a number is divisible by 2 then it is only prime if it actually is 2.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  13. #13
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    that's it im going to the ophthalmologist

  14. #14
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by cooper1200 View Post
    that's it im going to the ophthalmologist
    Just look at my previous post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Preferred answer length? Answer reuse value?
    By Nominal Animal in forum General Discussions
    Replies: 13
    Last Post: 10-29-2015, 01:19 PM
  2. Help with comparing answer to math problem and user's answer
    By Jake Garbelotti in forum C Programming
    Replies: 6
    Last Post: 07-20-2012, 10:12 PM
  3. new license-free lock-free data structure library published
    By Toby Douglass in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 12-22-2009, 02:33 AM
  4. Anybody get anything published ?
    By ed123 in forum C++ Programming
    Replies: 5
    Last Post: 12-07-2003, 07:18 AM

Tags for this Thread