# Check type of an integer

You will receive an integer less than 2000000000 and bigger than -2000000000 and you have to test what type(s) of number this is out of:

Factorial
Square
Cube
Prime
Composite
Triangular
Positive
Negative


Here is a definition of all the different types of numbers:

Factorial - A number That is equal to the product of all positive integers less than the a certain positive integer - 5×4×3×2×1 = 120
Square - A number that is the product of two identical integers - 5×5 = 25
Cube - A number that is the product of three identical integers - −5×−5×−5 = -125
Prime - A number that has only two divisors (1 and 0 are not) - 3
Composite - A number that has more than two divisors (also not 1 or 0) - 4
Triangular - A number that is equal the sum of all positive integers less than the a certain positive integer - 5+4+3+2+1 = 15
Positive - A number larger than 0 (not 0) - 7
Negative - A number less than 0 (not 0) - −7

### Test Cases

-8 = ["Cube","Negative"]
-2 = ["Negative"]
-1 = ["Cube","Negative"]
0 = ["Cube", "Square", "Triangular"]
1 = ["Cube","Factorial","Positive","Square","Triangular"]
2 = ["Factorial","Positive","Prime"]
3 = ["Positive","Prime","Triangular"]
4 = ["Composite","Positive","Square"]
5 = ["Positive","Prime"]
6 = ["Composite","Factorial","Positive","Triangular"]
7 = ["Positive","Prime"]
8 = ["Composite","Cube","Positive"]
9 = ["Composite","Positive","Square"]
10 = ["Composite","Positive","Triangular"]
11 = ["Positive","Prime"]
12 = ["Composite","Positive"]


Note: The Order of the Result Does Not Matter and it can be expressed with words (Does Not Have To Be A List)

Remember, this is a code golf so the smallest answer in bytes is what I'm looking for

• Welcome to PPCG. Nice first challenge. You may want harmonise the casing and/or explicitly state that any casing is acceptable. Does the result have to be a list of strings or is any indication using those words OK? Could we substitute "codes" or bit or synonyms, etc.?
Feb 26 '19 at 11:38
• Some more test cases would be nice too.
Feb 26 '19 at 11:53
• Is -1 a triangular number? Is -4 a composite number? Please define your types to make the challenge self contained and add some examples. Feb 26 '19 at 12:34
• Do we actually have to be able to handle ±2×10⁹ or is it enough that the algorithm in principle works. Some solutions may want to calculate impractically large factorials or have practical issues with finding primality of large numbers. You may want to allow limiting to what's representable.
Feb 26 '19 at 22:05
• Is 0 not a square and cube? Feb 27 '19 at 6:07

# APL (Dyalog Extended), 130 119 bytesSBCS

Anonymous tacit prefix function.

((∊∘⎕A⊂⊢)'FactorialSquareTriangularCubePositiveNegativePrimeComposite')/⍨{⍵∊¨(⊂∘!,×⍨⍮+\)⍳|⍵},(|∊⍳∘|*3⍨),<,>,1∘⍭,0∘⍭∧1∘<


Try it online!

()/⍨ filters the list of strings on the left by the bit mask on the right:

()'Facite' apply the following tacit function to the long string:

∊∘⎕A element-wise membership of the uppercase alphabet

⊂ partitions (i.e. True begins a new partition)

⊢ the unmodified argument

The last 46 bytes return a bit vector indicating [Factorial, Square, Triangular, Cube, Positive, Negative, Prime, Composite]:

{⍵∊¨(⊂∘!,×⍨⍮+\)⍳|⍵},(|∊⍳∘|*3⍨),<,>,1∘⍭,0∘⍭∧1∘<


Try it online!

{} the first three bits (factorial, square, triangular) come from this anonymous lambda:

⍵∊¨()… is the argument a member of each of the following three lists?

⊂∘! the enclosed factorials

, followed by

×⍨ the squares (lit. the multiplication selfies)

⍮ juxtaposed with

+\ the running sum

⍳ of the integers one through

|⍵ the absolute value of the argument

() the next bit (cube) comes from the following tacit function:

| is the absolute value of the argument

∊ a member of

⍳∘| the integers 1 through the absolute value of the argument

* raised to the power of

3⍨ three

< the next bit is simply whether it is positive (zero is less than)

> the next bit is simply whether it is negative (zero is more than)

1∘⍭ the next bit is whether it is prime

0∘⍭ the final bit is whether it is non-prime

∧

1∘< one is less than it

# JavaScript (ES7),  244 238  236 bytes

Returns an array of strings.

n=>'negative/positive/triangular/cube/square/factorial/prime/composite'.split/.filter((s,i)=>(k=!i)?n<0:i-1?i<5?(g=n=>(x=k++**(2+i%2))<n?g(n):x==n)(i-2?i&n<0?-n:n:8*n+1):(g=i-5?k=>n>1?n%--k?g(k):k<2^i>6:0:n=>n>1?g(n/++k):n==1)(n):n>0)


## How?

We filter an array of number properties, keeping track of our current position in the index $$\i\$$:

 i | Property   | OEIS    | Test
---+------------+---------+---------------------------------------------------------
0 | negative   | A001478 | n < 0
1 | positive   | A000027 | n > 0
2 | triangular | A000217 | look for k >= 0 such that k**2 = 8n+1
3 | cube       | A000578 | look for k >= 0 such that k**3 = |n|
4 | square     | A000290 | look for k >= 0 such that k**2 = n
5 | factorial  | A000142 | divide n by 1, 2, 3, etc. until it's either 1 or < 1
6 | prime      | A000040 | n > 1 and its largest proper divisor is equal to 1
7 | composite  | A002808 | n > 1 and its largest proper divisor is greater than 1

### Case $$\i=0\$$ (negative)

We test whether $$\n < 0\$$.

### Case $$\i=1\$$ (positive)

We test whether $$\n > 0\$$.

### Case $$\2\le i \le 4\$$ (triangular, cube, square)

We use the following function, starting with $$\k=0\$$:

g = n =>                   // n = input
(x = k++ ** (2 + i % 2)) // x = either k**3 if i = 3, or k**2 otherwise; increment k
< n ?                    // if the above result is less than n:
g(n)                   //   do a recursive call
:                        // else:
x == n                 //   test whether x is equal to n


If $$\i=2\$$ (triangular test), $$\g\$$ is called with $$\8n+1\$$ (i.e. we test whether $$\8n+1\$$ is a perfect square).

If $$\i=3\$$ (cube test), $$\g\$$ is called with $$\|n|\$$ (i.e. we test whether $$\|n|\$$ is a perfect cube).

If $$\i=4\$$ (square test), $$\g\$$ is called with $$\n\$$ (i.e. we test whether $$\n\$$ is a perfect square).

### Case $$\i=5\$$ (factorial)

We use the following function, starting with $$\k=0\$$:

g = n =>                   // n = input
n > 1 ?                  // if n is greater than 1:
g(n / ++k)             //   increment k and do a recursive call with n / k
:                        // else:
n == 1                 //   test whether n is equal to 1


### Case $$\i>5\$$ (prime, composite)

We use the following function:

g = k =>                   // k = current divisor, initialized to n
n > 1 ?                  // if n is greater than 1:
n % --k ?              //   decrement k; if k is not a divisor of n:
g(k)                 //     do a recursive call
:                      //   else:
k < 2 ^              //     test whether k = 1 (n is prime)
i > 6                //     invert the result if i = 7
:                        // else:
0                      //   always return 0 (n is neither prime nor composite)

• "its smallest proper divisor", maybe you mean "largest".
– tsh
Mar 4 '19 at 3:44

# Japt, 10196 88 bytes

-5 bytes thanks to Oliver
-8 bytes thanks to Shaggy

t„gÔ³dÖ%oÐÒdpoÐÓv‚šg…iv‚cu¼çyÜ)Ã$quÂÂpÎXqd g[U*8Ä ¬v1 Uk ÅÊU¬U<0Uõ³øU UõÊøU U¬v1 Uj]ð  Try it online! • Damn, beat me to doing it :) I need to be faster and online more if I'm ever going to answer anything :P Feb 27 '19 at 2:03 • You could always try using a different approach :P Feb 27 '19 at 2:05 • 96 bytes Feb 27 '19 at 3:32 • You've been posting some nice Japt answers recently. I have sort of relied on the Japt interpreter the few times I've posted a solution. That's pretty good if you are able to do it w/ TIO. – dana Feb 27 '19 at 4:59 • @dana I do use the interpreter every time, just because the quick reference is handy :P it's just that I move it to TIO so it's more consistent with other posts Feb 27 '19 at 5:02 # Stax, 79 71 bytes ë∙☺H▓»░g▬äεê▄ú▓▒º₧Σ↓i∟I∩├▼┬√&A4¼╔uë[Ü▼╒π←=Å♥\aâ╬°Æ■v⌂♂δ┴▄·╠.ë¶<→┬╪½¥6X  Run and debug it Unpacked, ungolfed, and commented, it looks like this. |c peek input and terminate if falsey 12{|Fm first 12 factorials # (1) is the input found in the array? x push original input from x register and G (2) execute program at trailing curly brace which tests for square-ness x push original input from x register |n get exponents of prime factorization |g gcd of array 3%! (3) is not a multiple of 3 x1|M push max(1, x) where x is the original input c copy value on the stack |p (4) is prime? s swap top two stack entries |fD (5) drop first element from prime factorization x8*^ push 8x+1 where x is the original input G (6) execute program at trailing curly brace which tests for square-ness x0> (7) is original input greater than zero? c! (8) logically negate the value at (7) ?|yL843smg!O5&;RVPC3&g9W!ISjvJN'n compressed literal for "negative positive triangular composite prime cube square factorial" - these are the names in reverse order of the test results pushed at (1)-(8) :.j title case and split on spaces f filter list of names and implicitly output using the rest of the program as a predicate d drop the name revealing the corresponding test result on top of the stack terminate program } G target from above; when done, resume execution c|qJ= copy, floor square-root, square, then test equality; this is a test for squareness  Run this one • How does this work? Mar 3 '19 at 6:38 • @lirtosiast: added an explanation Mar 4 '19 at 2:41 # Jelly, 676560 59 bytes “R!i“Æ²“ŒR*3i“Ẓ“<2oẒ¬“ŻÄi“R“<0”vTị“*Ṙœȯ#Ḅṙ×¬ɲṇṭY¤²nxẊ2ı»Ḳ¤  Try it online! A monadic link that accepts an integer and returns a list of strings. ## Explanation “R!i“Æ²“ŒR*3i“Ẓ“<2oẒ¬“ŻÄi“R“<0” First part of main link “ Begin list of strings separated by “ R Range: [1..n] ! Factorial [each] i Find index of argument “ , Æ² Is square? “ , ŒR Balanced range: [-n..n] *3 Cube [each] i Find index of argument “ , Ẓ Is prime? “ , <2 Is less than 2? o Or Ẓ Is prime? ¬ Negate “ , Ż Range with zero: [0..n] Ä Prefix sums i Find index of argument “ , R Range [used to test for positive] “ , <0 Is less than 0? ” End list of strings vTị“*Ṙœȯ#Ḅṙ×¬ɲṇṭY¤²nxẊ2ı»Ḳ¤ Second part of main link v Evaluate each with the argument T Indices of truthy elements ị Index into ¤ ( “*Ṙœȯ#Ḅṙ×¬ɲṇṭY¤²nxẊ2ı» Compressed string "Factorial Square Cube Prime Composite Triangular Positive Negative" Ḳ Split on spaces ¤ )  Thanks to caird for 60->59 • -1 byte (+\ -> Ä) Nov 13 '20 at 22:58 • @cairdcoinheringaahing Thanks! I changed this part so many times that I totally forgot about this. Nov 13 '20 at 23:14 # 05AB1E, 76 75 bytes _i¯ë.±D(IÅ²IÄ©3zmDïQIpIÑg3@Id*®LηPIå®ÅTIå)”îË¢…ï©×Ä›ˆÉíšâialÓª”¨¨"ular"«#sÏ  Explanation: _i # If the (implicit) input is 0: ¯ # Push an empty list ë # Else: .± # Push the signum of the (implicit) input (-1 if <0; 0 if 0; 1 if >0) # i.e. -8 → -1 D( # Duplicate and take it's negative # i.e. -1 → 1 IÅ² # Push the input, and check if it's a square # (negative values will result in the negative numbers themselves) # i.e. -8 → -8 IÄ # Push the absolute value of the input # i.e. -8 → 8 © # Store it in the register (without popping) to reuse later 3z # Push 1/3 m # Take the absolute input to the power of 1/3 # (NOTE: a^(1/3) is the same as ³√a) # i.e. 8 → 2.0 DïQ # Check if this is an integer without decimal digits # i.e. 2.0 → 1 (truthy) Ip # Push the input, and check if it's a prime # i.e. -8 → 0 (falsey) IÑ # Push the input, and get a list of its divisors # i.e. -8 → [1,2,4,8] g # Take the length of that list # i.e. [1,2,4,8] → 4 3@ # And check if it's larger than or equal to 3 # (the list includes 1, hence the check for >=3 instead of >=2) # i.e. 4 >= 3 → 1 (truthy) Id # Push the input again, and check if it's non-negative # i.e. -8 → 0 (falsey) * # Check if both are truthy # i.e. 1 and 0 → 0 (falsey) ®L # Create a list in the range [1, absolute value of the input] # i.e. 8 → [1,2,3,4,5,6,7,8] η # Get a list of prefixes of this list # → [[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5],[1,2,3,4,5,6],[1,2,3,4,5,6,7],[1,2,3,4,5,6,7,8]] P # Get the product for each prefix-list # → [1,2,6,24,120,720,5040,40320] Iå # Check if the input is in this list # i.e. [1,2,6,24,120,720,5040,40320] and -8 → 0 (falsey) ®ÅT # Create a list of all triangular numbers below or equal to # the absolute value of the input # i.e. 8 → [0,1,3,6] Iå # Check if the input is in this list # i.e. [0,1,3,6] and -8 → 0 (falsey) ) # Wrap all values on the stack into a single list # → [-1,1,-8,1,0,0,0,0] ”îË¢…ï©×Ä›ˆÉíšâialÓª” # Push dictionary string "Positive Negative Square Cube Prime Composite Factorial Triangle" ¨¨ # Remove the last two characters (the "le" of "Triangle") "ular"« # Append "ular" # # Split this string by spaces s # Swap to take the earlier created list again Ï # And only leave the strings at the truthy indices # (NOTE: Only 1 is truthy in 05AB1E) # i.e. [-1,1,-8,1,0,0,0,0] → ["Negative","Cube"] # (and output the result implicitly)  See this 05AB1E tip of mine (section How to use the dictionary?) to understand why ”îË¢…ï©×Ä›ˆÉíšâialÓª” is "Positive Negative Square Cube Prime Composite Factorial Triangle". # Husk, 606561 59 bytes Edit: +5 bytes to fix zero as a square, cube & triangular, and to fix zero, one and negative numbers as non-composites (in line with test cases)... ...but then -4 bytes thanks to Razetime, by changing all the forks (§: and §e) to a mapping over a bigger list of functions f+m€⁰mëmΠm□m^3mΣΘḣ⁰mëṗȯ>2LḊ>0<0⁰w¨◄Γq□ṡ_ẏĊ8ζŀ⁸→PṘβ□θpż√₁□Ġë  Try it online! How? f # select each element if input satisfies: + # combine 2 lists of 4 together: m€⁰§:mΠ§:m□§em^3mΣΘḣ⁰ # first list: m€⁰ # check whether input is §: §: §e # among mΠ # factorials of m□ # or squares of m^3 # or cubes of mΣ # or triangular numbers of Θḣ⁰ # series from 0..input mëṗȯ>2LḊ>0<0⁰ # second list: m ⁰ # map functions over input: ë # list of 4 functions ṗ # is prime? ȯ>2LḊ # is composite (more than 2 divisors)? >0 # is positive? <0 # is negative? # elements to select: w # split on spaces ¨◄Γq□ṡ_ẏĊ8ζŀ⁸→PṘβ□θpż√₁□Ġë # compressed string # "Factorial Square Cube Triangular Prime Composite Positive Negative"  • 55(mapping list of functions twice) Nov 14 '20 at 1:50 • Incorrectly classifies all negative numbers, 0 and 1 as composite and doesn't classify 0 as a square, cube and triangular. Nov 14 '20 at 8:27 • @xigoi - well spotted! Fixed now (with negatives as non-composite in line with test-cases). Nov 14 '20 at 10:32 • @Razetime - thanks! The mapping is really nice! Swapping mΣ for ∫ unfortunately seems to result in negative numbers being classified as triangular: I don't understand why, so I had to stick with mΣ (and so annoyingly still be 2 bytes longer than Jelly...) Nov 14 '20 at 10:49 • @DominicvanEssen happens sometimes lol Nov 14 '20 at 12:53 # Python 3, 405 259 bytes Thanks to @ASCII-only def z(n): x=y=1 while x<n:x*=y;y+=1 return"Factorial "*(x==n)+"Square "*(n>=0and n**.5%1==0)+"Cube "*(abs(n)**(1/3)%1==0)+"Negative "*(n<0)+"Positive "*(n>0)+"Triangular "*(n>0and(8*n+1)**.5%1==0)+["Composite ","Prime "][all(n%x for x in range(2,n))]*(n>1)  Try it online! Old Code Generates the squares, cubes, and factorials into lists to check against, has a function to check for primality (and consequently compositeness), uses a simple equality to check for positivity/negativity, and checks if 8n+1 is a perfect square to determine if n is a triangular number. Hardcoded the limits for the square, cube, and factorial lists. Returns the answer in a set, which I assume is acceptable (adding elements to a list requires more bytes). p=lambda n:all(n%x!=0 for x in range(2,n)) def z(n): f,a,x,y,s,c=[],set(),1,1,[i*i for i in range(44721)],[i**3 for i in range(-1259,1259)] while x<2*10**9:f.append(x);x*=y;y+=1 if n in f:a.add("Factorial") if n in s:a.add("Square") if n in c:a.add("Cube") if n>0: a.add("Positive") if 8*n+1 in s:a.add("Triangular") if n>1:a.add(("Composite","Prime")[p(n)]) if n<0:a.add("Negative") return a  Try it online! • jZNfa6swGMav66fIhIHaDIztztpuDsb@wIExBufciRepjV3ARpfEzXbss/ck0Wqxx8NBLzS/53neNy9JsZVvOZvs90WY4c1yhQFb4Cxz2Hl1FvogzTmoAGWAY7YmTgCZ61orkoKdw9yFNUohhhXcQgGTMIqhepF6IupRY6WddTq9CpAba@ZN@hAFl3M3tkafbzQjoLoJPOR73nyRjsOoiq8rL9xeb8chskY0BUz70gVWzH7Cicw5xZkdd0zU7Nd7iTlpAF4K1bGCSQ3vy@UBsVtf7WRkll9zQSX9MEizmcfG6Cjzt6rF1mWGeWtGhjj2fb4ptJvY0H7ldENsNypUzbgR3vh1xAtZ40MJTmTJGcB7SYRMsCAChCCynIsZBF91j7AzfLtQoUCj3hoalPuaNJOAoNYYcGzppgi7CcDWdrxr4zUtDJjqrRvZRMtO0Gnc1LRyPL6THozu8q95Bv3oRwx01y99NRw560c2w2rVRjX/z96R/w9hvyuEhttCwWCQ5rFlmbvFilJCgJn4JPoPtAdMHfWCUyadRrKrP9S11qdUEOm0K@AsbBL0/ahd9tPdz@fHBwhIVZBEkpV9qOLu/wA Feb 27 '19 at 6:21 • 381, add https://tio.run/## to start Feb 27 '19 at 6:21 • Feb 27 '19 at 6:23 • 358 Feb 27 '19 at 6:23 • 259 Feb 27 '19 at 7:31 # C# (Visual C# Interactive Compiler), 286 bytes n=>{int k=0;return(n>0?"Positive":n<0?"Negative":"")+(Math.Sqrt(8*n+1)%1==0?"Triangular":"")+(new int[n].Where((a,b)=>n%++b<1).Count()>2?"Composite":n<3?"":"Prime")+(Math.Pow(n,1/3d)%1>0?"":"Cube")+(Math.Sqrt(n)%1>0?"":"Square")+(g(k)?"Factorial":""));bool g(int i)=>i>1?g(i/++k):1==i;}  Try it online! # 05AB1E, 63 bytes ÅTIL3mILÄ!)IQOÎ‹Î›IÅ²IpD≠$‹*)“×ÄšâialîË¢…ï©›ˆÉí“#.•’αë\Ä•šsÏ™


Commented:

ÅT                   # push the triangular numbers up to the input
IL                 # push the range [1..input]
3m               # take each number to the third power
IL             # push the range [1..input]
Ä            # take absolute values
!           # take the factorial of each number
)          # collect the three list in a list of lists
IQ        # for each element chack if it is equal to the input
O       # sum the sublists
      # and push all three values to the stack

Î‹                   # is 0 < input? (Î pushes 0 and the input)
Î›                 # is 0 > input?
IÅ²              # is the input a square number?
Ip            # is the input prime?
D≠          # duplicate this value and invert the copy
\$         # push 1 and the input
‹        # is 1 < input
*       # is the input composite? (not prime & > 1)
)      # collect all these values in a list

“×ÄšâialîË¢…ï©›ˆÉí“  # push compressed string "cube factorial positive negative square prime composite"
#                   # split on spaces
.•’αë\Ä•          # push compressed string "triangular"
š         # and prepend it to the word list
s        # swap the integer back to the top
Ï       # keep all words where 1's are in the integer list
™      # titlecase all words