# Challenge:

Given an array of non-negative whole numbers numbers in the range of 0 to Infinity, Check whether all of them are primes or not. (You can take input as a string too if you want)

# Input:

Input: An array of numbers

Output: The array with every element replaced by one of these:

-1                 -----> If 0, 1
1                  -----> If it is a prime number greater than 1
the highest factor -----> If that number is not prime

Return either -1 (0, 1), 1 (for primes >= 2) or the highest factor of given number (for non-primes)

# Examples:

[1, 2, 3, 4, 10, 11, 13]                        ---> [-1, 1, 1, 2, 5, 1, 1]
[100, 200, 231321, 12312, 0, 111381209, 123123] ---> [50, 100, 77107, 6156, -1, 1, 41041]

# Note:

Input will always be valid, i.e it will consist only of numbers and decimals are not tested for. The array can be empty, if so, return the empty array.

# Restriction:

This is so shortest code in bytes for each language wins.

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

• I highly recommend using the Sandbox for future questions, to provide feedback on questions before posting them – Jo King May 1 '18 at 9:47
• @Joking : for infinity you must output all numbers upto infinity. This is just for you and you also have to make sure it doesn't time out or anything. JK : time out error is the most likely thing you will get for infinity – user79855 May 1 '18 at 9:49
• just wanted to note that in "If it is a prime number greater than 1" greater than 1 really is not necessary because prime numbers are always greater than 1 – Ivo Beckers May 1 '18 at 10:59
• Define highest factor. Should I return the number itself? The highest divisible prime? The highest factor that isn't itself? – Nissa May 1 '18 at 15:39
• I take it our programs are only required to work for integers up to our chosen language's max integer size (for those that don't have support for arbitrarily large integers) – JDL May 1 '18 at 16:18

# Jelly,  7 6 bytes

ÆḌṪ€o-

A monadic link accepting a list of non-negative integers and returing a list of integers greater than or equal to -1.

Try it online!

### How?

Note that:

• All primes have a single proper divisor (one)
• All composites have multiple proper divisors (one plus the others)
• No number has itself as a proper divisor
• Jelly's get-proper-divisors atom, ÆḌ, yields a list of proper divisors in ascending order
• Zero and one have no proper divisors (they are neither prime, nor composite)
• Applying Jelly's tail atom, , to an empty list yields zero
• No number has a proper divisor of zero (let alone being the maximal one)
• All non-zero numbers are truthy in Jelly, while zero is falsey

ÆḌṪ€o- | Link: list of integers   e.g. [ 0, 1,  2,  5,     10,    5183]
ÆḌ     | proper divisors (vectorises)  [[],[],[1],[1],[1,2,5],[1,71,73]]
Ṫ€   | tail €ach                     [ 0, 0,  1,  1,      5,      73]
- | literal minus one
o  | logical OR (vectorises)       [-1,-1,  1,  1,      5,      73]

# Jelly, 9 8 bytes

Saved 1 byte thanks to @Dennis

:ÆfṂ€$~~ ### Commented We take advantage of the fact that both nan and inf become 0 in Jelly when a bitwise NOT is applied to them. :ÆfṂ€$~~ - main link, taking the input list
ÆfṂ€$- treat these two links as a monad: Æf - get the lists of prime factors (0 --> 0; 1 --> empty list; prime --> itself) € - for each list, Ṃ - isolate the minimum prime factor (turns empty lists into 0) : - divide each entry by its minimum prime factor (0/0 --> nan; 1/0 --> inf) ~~ - bitwise NOT x2 (nan or inf --> 0 --> -1; other entries are unchanged) • You didn't use JavaScript this time ? nice answer btw – user79855 May 1 '18 at 8:44 • I really like the ~~. :ÆfṂ€$~~ saves a byte by eliminating the helper link. – Dennis May 1 '18 at 13:51

Try it online!

# Java 8, 105103 87 bytes

a->{for(int i=a.length,n,x;i-->0;a[i]=n<2?-1:n/x)for(n=a[i],x=1;++x<n;)if(n%x<1)break;}

Modifies the input-array instead of returning a new one to save bytes.

Try it online.

Explanation:

a->{                  // Method with integer-array parameter and no return-type
for(int i=a.length,n,x;i-->0;
//  Loop backward over the array
a[i]=           //    After every iteration: change the current item to:
n<2?       //     If the current item is 0 or 1:
-1        //      Change it to -1
:          //     Else:
n/x)      //      Change it to n divided by x
for(n=a[i],      //   Set n to the current item
x=1;++x<n;)  //   Inner loop x in range [2,n)
if(n%x<1)      //    If n is divisible by x:
break;}      //     Stop the inner loop (x is now the smallest prime-factor)
//   (if the loop finishes without hitting the break,
//    it means n is a prime, and x and n will be the same)

• /: \w+$/c1 Numbers with one prime factor (themselves) are prime. Replace these with 1 • s%: %/% Replace : with /. This builds an arithmetic expression to divide the (non-prime) input number by its smallest prime factor to give the largest factor • y/ /#/ Remove the list of other (unneeded) factors (by commenting out) • bc Arithmetically evaluate and display Try it online! • You might be able to drop the -r, and for the first two s's you can use /regex/cvalue to golf a byte, simplifying this regex further can save more, and you can save a byte in the last two regex's by only replacing the : with the /, and then commenting out the unwanted part, like so, tio.run/##JYlBCoMwFET3c4qABhdSfuZ/… – user41805 May 1 '18 at 17:51 • @Cowsquack very good - thanks! – Digital Trauma May 1 '18 at 18:01 # Python 2, 61 59 bytes lambda x:[max(k for k in[-1]+range(1,n)if n%k<1)for n in x] Try it online! Improvements # JavaScript (Node.js), 37 bytes x=>x.map(a=>(f=t=>a%--t<1?t:f(t))(a)) Try it online! Recursive, stack overflow for large input # JavaScript (Node.js), 41 bytes x=>x.map(a=>eval('for(t=a;a%--t!=0;);t')) Try it online! # Pari/GP, 37 bytes a->[if(x<2,-1,x/divisors(x)[2])|x<-a] Try it online! # Racket, 105 bytes (λ(L)(map(λ(n)(if(< n 2)-1(p n(- n 1))))L))(define(p n d)(if(= d 1)1(if(=(modulo n d)0)d(p n(- d 1))))) Try it online! # Befunge-98 (FBBI), 39 bytes j&:!+f0p1-1:::' -:!j;3k$.nbj;-\%!!j:1+a

Try it online!

Ends with the & when there are no more numbers. This causes the program to stall for 60 seconds until TIO ends the program. This is unavoidable for Befunge-98, at least on TIO because both interpreters do this. After hitting play, you can stop the program after a bit in order to see what would be output if you did wait the minute.

Essentially, for every new number, if it is 0, it turns it into a 1. Then it puts a -1 onto the stack followed by a number that starts from 1 and counts up until it reaches the input number, in which case it prints out the second number on the stack (-1 for an input of 0 or 1, and the highest factor for others). Every time through the loop, we add the value of the iterator to the stack behind it if (input % iterator == 0). This means that when we get to the input, we just have to throw away the iterator and print. Then, we clear the stack with n and return to the read input function.

I may expand of the explanation later, we'll see...

# Retina 0.8.2, 33 bytes

%(^0|^1$-1 \d+$*
^(1+)\1+.1

Try it online! Link includes those test cases that aren't too slow. Explanation:

%(

Loop over each input number.

^0|^1$-1 Special-case 0 and 1. \d+$*

Convert to unary (doesn't affect -1).

^(1+)\1+.1

Replace each number with its largest proper factor in decimal.

# tinylisp, 75 bytes

(q((L)(map(q((N)(i(l N 2)(- 1)(/ N(min(prime-factors N))))))L

Try it online! (Contains 4 extra bytes to give the anonymous function a name so we can call it in the footer.)

### Ungolfed/explanation

Observe that returning 1 for prime $$\n\$$ and the largest factor less than $$\n\$$ for composite $$\n\$$ can be combined into returning $$\n/p\$$ where $$\p\$$ is the smallest prime factor of $$\n\$$.

(load library)               Library gives us map, -, /, min, and prime-factors functions

(lambda (L)                  Anonymous function, takes a list of numbers L
(map                         Map
(lambda (N)                  Anonymous function, takes a number N
(if (less? N 2)              If N < 2
(- 1)                        -1; else
(/ N                         N divided by
(min                        the minimum
(prime-factors N)))))      of the prime factors of N
L)))                        ... to L