Wissam A Jackal
Wissam A Jackal

Reputation: 23

Palindromic Wing Primes - Mathematica

definition : Palindromic Wing Primes (or PWP's for short) are numbers that are primes, palindromic in base 10, and consisting of one central digit surrounded by two wings having an equal amount of identical digits and different from the central one. E.g.

101 99999199999 333333313333333 7777777777772777777777777 11111111111111111111111111111111411111111111111111111111111111111

A number is a palindromic wing prime if it is both a palindromic wing and prime. Here are the first several palindromic wing primes: 101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929, 11311, 11411, 33533, 77377, 77477, 77977, 1114111, 1117111, 3331333, 3337333, 7772777, 7774777, 7778777, 111181111, 111191111, 777767777, 77777677777,...

Please i need help with find the right algorithm or pseudo code for if a number is palindromic wing and if someone could help me with guid me to more info about the Palindromic Wing Primes , their history and the last results and maybe help me with "mathematica Programming " that would be amazing

kind regards

Upvotes: 0

Views: 255

Answers (2)

Chris Degnen
Chris Degnen

Reputation: 8655

Testing wing palindromes from wing length 1 to 20 using Mathematica.

sets = DeleteCases[Tuples[Range[0, 9], 2], {a_, a_} | {0, _}];

grow[n_] := Map[Flatten, {a = ConstantArray[#1, n], #2, a} & @@@ sets]

test[c_] := If[PrimeQ[k = FromDigits@c], AppendTo[output, k]]

run[from_, to_] := Do[test /@ grow[i], {i, from, to}]

output = {};
run[1, 20]
101
131
151
181
191
...
111111111111111111131111111111111111111
777777777777777777797777777777777777777
77777777777777777777977777777777777777777

Upvotes: 1

Copperfield
Copperfield

Reputation: 8510

Interesting definition, first time that I hear about that.

Assuming that you know how to check if a number is palindromic and prime, here is some pseudo and python code

isPalindromicWing(N){
    if isPalindromic(N){
        num <- toString(N)
        tam <- length(num)
        if isOdd(tam) and lenght(toSet(num)) = 2{
            middle <- num[ floor(tam/2) ] 
            if 1 = num.count(middle){
                return True
            }
        }
     }
     return False
}

isPWP(N){
    return isPalindromicWing(N) and isPrime(N)
}

I use sets to remove repetitions, and as the number can only have 2 different digit with lenght(toSet(num)) == 2 I check for that, then I take the middle number and check if only there is one of it in the number. The rest I think is self explanatory.

in python that is

def isPalindromic(N):
    num = str(N)
    return N == int( num[::-1] )

def isPalindromicWing(n):
    if isPalindromic(n):
        num = str(n)
        tam = len(num)
        if tam % 2 == 1 and len(set(num)) == 2:
            middle = num[tam // 2]
            if 1 == num.count(middle):
                return True 
    return False

I don't know about "mathematica Programming", but is you understand this code surely you can do it in that too

Upvotes: 0

Related Questions