| Bytes | Lang | Time | Link |
|---|---|---|---|
| 023 | APL NARS | 250110T102047Z | Rosario |
| 008 | Jelly | 180305T205439Z | Jonathan |
| 044 | JavaScript V8 | 250110T142553Z | l4m2 |
| 014 | Japt | 180306T101330Z | Shaggy |
| 036 | Add++ | 180403T123106Z | caird co |
| 084 | Python 3 | 180307T170927Z | Dat |
| 012 | MATL | 180306T004514Z | Luis Men |
| 046 | Haskell | 180307T025623Z | xnor |
| 011 | 05AB1E | 180306T083351Z | Emigna |
| 048 | Ruby | 180305T192050Z | Asone Tu |
| 052 | Python 2 | 180305T200112Z | xnor |
| 022 | J | 180305T210845Z | cole |
| 048 | Haskell | 180305T194912Z | Wheat Wi |
| 045 | Haskell | 180305T202711Z | nimi |
| 010 | Pyt | 180305T200506Z | mudkip20 |
| 043 | R | 180305T192300Z | Giuseppe |
APL (NARS), 23 chars
{⍵∊-/¨,k∘.,3×k←,+\0,⍳⍵}
If ⍵ is the input, all the elements of the set k with "k←,+\0,⍳⍵" are all triangulars
numbers less or equal to "2÷⍨⍵×⍵+1".
Than this function subtract each element of k each element of 3×k, see if ⍵ is in that set.
If it is in it, return 1 (truncated triangular) else return 0.
Could be some bug because i don't know if that set k is sufficient big
for find the solution. It could be a bug because i search a positive number in an array almost all compose of negative numbers, the inverse subtract each element of 3×k each element of k doesnt work. It seems ok until 100.
For example k=0 1 3 6 are triangular numbers, 3xk=0 3 9 18, and we see that 3-1=2 and 2 is in each element (3xk) - each element of k.
Test:
(0,⍳15)/⍨{⍵∊-/¨,k∘.,3×k←,+\0,⍳⍵}¨(0,⍳15)
0 1 3 6 7 10 12 15
Jelly, 10 8 bytes
-2 bytes using builtins added to Jelly around two months after this challenge was created (+\ -> Ä and 0r -> Ż).
ŻÄð_÷3f⁸
A monadic link accepting an integer and returning a truthy value (a non empty list) or a falsey value (an empty list).
Try it online! (footer performs Python representation to show the [0] results as they are)
...or see a test-suite (runs for 0 to 20 inclusive)
How?
Given N, forms the first N triangle numbers, subtracts N from each, divides each result by 3 and keeps any results that are one of the first N triangle numbers.
ŻÄð_÷3f⁸ - Link: integer, N e.g. 7
Ż - zero range N [ 0, 1, 2, 3, 4, 5, 6, 7]
Ä - cumulative sums of that [ 0, 1, 3, 6, 10,15, 21, 28]
ð - start a new dyadic link with that, t, on the left and N on the right
_ - t subtract N (vectorises) [ -7,-6,-3, -1, 3, 8, 14, 21]
÷3 - divide by three (vectorises) [-2.33,-2,-1.33,-0.33,1,2.67,4.67, 7]
⁸ - chain's left argument, t [ 0, 1, 3, 6, 10,15, 21, 28]
f - filter keep [ 1 ]
- a non-empty list, so truthy
Japt, 16 14 bytes
ò å+
ï-Um*3)øN
ò å+\nï-Um*3)øN :Implicit input of integer U
ò :Range [0,U]
å+ :Cumulatively reduce by addition
\n :Reassign to U
ï- :Cartesian product with, reducing each pair by subtraction
Um : Map U
*3 : Multiply by 3
) :End cartesian product
ø :Contains any element of
N : Array of all inputs
Python 3, 84 bytes
lambda n:1in set([((8*(n+(i*(i+1)/2)*3)+1)**0.5)%4==1for i in range(n)])or n in[0,1]
MATL, 12 bytes
tQ:qYst!3*-m
Outputs 1 for truthy, 0 for falsy.
Try it online! Or verify all test cases.
How it works, with example
Consider input 6
t % Implicit input. Duplicate
% STACK: 6, 6
Q:q % Increase, range, decrease element-wise. Gives [0 1 ... n]
% STACK: 6, [0 1 ... 6]
Ys % Cumulative sum
% STACK: 6, [0 1 3 6 10 15]
t! % Duplicate, transpose
% STACK: 6, [0 1 3 6 10 15], [0;
1;
3;
6;
10;
15]
3* % Times 3, element-wise
% STACK: 6, [0 1 3 6 10 15 21 28 36 45 55], [0;
3;
9;
18;
30;
45]
- % Subtract, element-wise with broadcast
% STACK: 6, [ 0 1 3 6 10 15 21;
-3 -2 0 3 7 12 18;
-9 -8 -6 -3 1 6 12;
-18 -17 -15 -12 -8 -3 3;
-30 -29 -27 -24 -20 -15 -9;
-45 -44 -42 -39 -35 -30 -24;
-63 -62 -60 -57 -53 -48 -42]
m % Ismember. Implicit display
% STACK: 1
Haskell, 46 bytes
f n=or[mod(gcd(p^n)(4*n-1)-5)12<3|p<-[1..4*n]]
Having thrown a bunch of number theory at the problem (thanks @flawr), I found this characterization:
n is a truncated triangular number exactly if in the prime factorization of 4n-1, any prime of the form 5 mod 12 or 7 mod 12 appears an even number of times.
This means, for instance, that 4n-1 may not be divisible by 5 unless it's further divisible by 52=25 and the total number of 5 factors is even.
Haskell doesn't have a factorization-built-in, but we can improvise. If we work with factorizations into primes powers like 12=3*4, we can use the equivalent statement:
n is a truncated triangular number exactly if the factorization of 4n-1 into prime powers has no terms of form 5 mod 12 or 7 mod 12.
We can extract the power of a prime p appearing in k as gcd(p^k)k. We then check that the result r is not 5 or 7 modulo 12 as mod(r-5)12>2. Note that r is odd. We also check composites as p, lacking a way to tell them from primes, but the check will pass as long as its factors do.
Finally, negating >2 to <3 and switching True/False in output saves a byte by letting us use or instead of and.
A related characterization is that the divisors of 4n-1 taken modulo 12 have more total 1's and 11's than 5's and 7's.
53 bytes
f n=sum[abs(mod k 12-6)-3|k<-[1..4*n],mod(4*n)k==1]<0
05AB1E, 11 bytes
ÅT3*+8*>ŲZ
Explanation
ÅT # get a list of triangle numbers upto input
3* # multiply each by 3
+ # add input to each
8* # multiply each by 8
> # increment each
Ų # check each for squareness
Z # max
This is based on the fact that a number T is triangular if 8T+1 is an odd perfect square.
We start on the list of triangles we could truncate, calculate the possible larger triangles based on them and check if it in fact is triangular.
Ruby, 65 57 52 48 bytes
->n,b=0{b+=1;(8*n-2+3*b*b)**0.5%1==0||b<n&&redo}
Inspired by xnor's python answer
Python 2, 52 bytes
f=lambda n,b=1:b>n+1or(8*n-2+3*b*b)**.5%1>0<f(n,b+1)
Outputs True/False flipped. Uses this characterization:
n is a truncated triangular number exactly if 8n-2 has form a2-3b2 for some integers a,b.
We check whether any 8*n-2+3*b*b is a perfect square for any b from 1 to n+1. We avoid b=0 because it gives an error for a square root of a negative when n==0, but this can't hurt because only odd b can work.
Done non-recursively:
Python 2, 53 bytes
lambda n:0in[(8*n-2+3*b*b)**.5%1for b in range(~n,0)]
J, 22 bytes
e.[:,@(-/3*])2![:i.2+]
Straightforward and somewhat-poorly-golfed approach.
Explanation
e.[:,@(-/3*])2![:i.2+]
2![:i.2+] Range of triangular numbers up to N
(-/3*]) All possible subtractions of 3T from T
where T is triangular up to the Nth triangular number
,@ Flattened into a single list
e. Is N in the list?
Pyt, 10 bytes
Đř△Đ3*ɐ-Ƒ∈
Explanation:
Implicit input
Đ Duplicate input
ř Push [1,2,...,input]
△ For each element k in the array, get the kth triangle number
Đ Duplicate the top of the stack
3* Multiply by 3
ɐ ɐ - All possible:
- subtractions between elements of the two arrays
Ƒ Flatten the nested array
∈ Is the input in the array
R, 45 43 bytes
-2 bytes thanks to Vlo
(n=scan())%in%outer(T<-cumsum(0:n),3*T,"-")
I'm fairly sure we only need to check the first n triangular numbers for this; brute force checks if n is in the pairwise differences of the triangular numbers and their triples.