# Squarefree Palindromes [closed]

Create the shortest function, program, or expression that calculates a sequence of squarefree palindromic numbers.

A squarefree number is one which is not evenly divisible by a square number (i.e. does not contain a repeated prime factor). For example, $$\44 = 2^2 \times 11\$$ is not squarefree, whereas $$\66 = 2\times3\times11\$$ is.

You can find a list of the numbers from this link. The list goes as such: 1, 2, 3, 5, 6, 7, 11, 22, 33, 55, 66, 77, 101, 111, 131, 141, 151, 161, 181, 191...

• @user The challenge does say Create the shortest function, program, or expression. An expression is not a standard format for code-golf, however. – Arnauld Dec 18 '20 at 19:10
• Am I missing something in the terminology here? If it's a prime factorization, all the factors must be primes. Thus, none of them can be a square. I think I understand what's being sought here (no two identical prime factors), but it should be stated explicitly. – Xcali Dec 18 '20 at 19:44
• @user, then the question is different: What palendromic numbers are not perfect squares? – Xcali Dec 18 '20 at 19:46
• I've edited the question slightly to include the definition of a squarefree number, as well as examples. However, until you define "calculates a sequence of", this is still unclear. I'd recommend going by our standard sequence rules – caird coinheringaahing Dec 18 '20 at 20:48
• I'm not clear, does the sequence need to list every squarefree palindrome, or just be any infinite sequence of such numbers, presumably distinct? – xnor Dec 18 '20 at 21:29

# Python 2, 82 75 bytes

n=0
while 1:
n+=1
ifn[::-1]==n*all(n%i**2for i in range(2,n)):print n


Try it Online!

If this was a decision problem (54 bytes):

lambda n:n[::-1]==n*all(n%i**2for i in range(2,n))


### Explanation:

n[::-1]==n                     # If n is a palindrome. n is repr(n).
# We check that it's the same
# backwards and forwards.
*                    # Multiplplying 2 booleans is AND
all(n%i**2for i in range(2,n))     # Check that squares of all #'s < n do not divide n

• Your condition can be shortened to n[::-1]==n*all(n%i**2for i in range(2,n)). – ovs Dec 18 '20 at 22:25
• @ovs Nice one! Thanks. – mbomb007 Dec 18 '20 at 22:34

# Wolfram Language (Mathematica), 52 bytes

Do[If[SquareFreeQ@n&&PalindromeQ@n,Print@n],{n,âˆž}]


Try it online!

# 05AB1E, 8 bytes

Prints the infinite sequence.

âˆžÊ’Ã‚QyÃ“Ã *


Try it online!

Commented:

âˆž        # push the list of natural numbers [1, 2, ...]
Ê’       # keep the values for which the following is 1:
Ã‚      #   push the number and its reverse
Q     #   are both equal?
y    #   push the number again
Ã“   #   push the exponents of the prime factorisation
Ã   #   take the maximum
* #   multiply both numbers


# Husk, 8 bytes

foS=upÄ°â†”


Try it online!

# Brachylog, 5 bytes

Generates the sequence

á¸‹â‰ &â†”?


Try it online!

á¸‹â‰ &â†”? (the input's)
á¸‹     prime decomposition
â‰     has only unique elements
&   and the input
â†”  reversed
? is the input
(and also the output)


# Jelly, 10 bytes

1Ã†fQÆ‘Ã—Å’á¸‚Æ²#


Try it online!

Returns the first n squarefree palindromes

## How it works

1Ã†fQÆ‘Ã—Å’á¸‚Æ²# - Main link. Takes no arguments
Ã†f        -   Prime factorisation of k (with repeats)
Æ‘      -   Is this invariant under:
Q       -     Deduplication
Å’á¸‚   -   Is k a palindrome?
Ã—     -   Both conditions are true?
1        # - Read an integer n from STDIN. Count up k = 1, 2, 3, ...
until n such k return true under f(k). Return those k


# JavaScript (ES7), Â 81Â  76 bytes

Returns the n-th term, 1-indexed.

f=(n,i)=>(g=d=>i[d]&&i[d]-i[k++]?1:i%d--**2?g(d):d)(i+=k='')||n--?f(n,-~i):i


Try it online!

## Commented

### Helper function

The helper function g checks simultaneously whether i is palindromic and squarefree. The variable d is used as a counter to test the divisors and as a decreasing digit index. The variable k is used as an increasing digit index. Both i and k are defined in the wrapper.

g = d =>            // g is a recursive function taking a counter d
i[d] &&           //   if i[d] is defined
i[d] - i[k++] ?   //   and it's not equal to i[k] (increment k afterwards):
1               //     i is not palindromic: force a truthy result
:                 //   else:
i % d-- ** 2 ?  //     if dÂ² is not a divisor of i (decrement d afterwards):
g(d)          //       do a recursive call
:               //     else:
d             //       return d (0 if i is squarefree)


### Wrapper

The main function decrements n whenever g returns 0 and stops when n = 0.

f = (n, i) =>       // f is a recursive function taking an index n
g(i += k = '') || //   set k to a zero'ish empty string,
//   coerce i to a string
//   and invoke g with d = i
n-- ?             //   if the above call was truthy or n is not equal to 0:
f(n, -~i)       //     do a recursive call with i + 1
:                 //   else:
i               //     success: return i


# Perl 5-MList::Util=all, 52 bytes

$_-$r||(all{$r%$_**2}2..$_)&&say while$r=reverse++\$_


Try it online!