R, (削除) 69 (削除ここまで) 58 bytes
function(n)3-n%in%(l=cumsum(1:n))-n%in%outer(c(0,l),l,"+")
Explanation:
function(n){
T <- cumsum(1:n) # first n triangular numbers [1,3,6]
S <- outer(c(0,T),T,"+") # sums of the first n triangular numbers,
# AND the first n triangular numbers [1,3,6,2,4,7,4,6,9,7,9,12]
3 - (n %in% S) - (n %in% T) # if n is in T, it's also in S, so it's 3-2: return 1
# if n is in S but not T, it's 3-1: return 2
# if n isn't in S, it's not in T, so 3-0: return 3
}
R, (削除) 69 (削除ここまで) 58 bytes
function(n)3-n%in%(l=cumsum(1:n))-n%in%outer(c(0,l),l,"+")
Explanation:
function(n){
T <- cumsum(1:n) # first n triangular numbers [1,3,6]
S <- outer(c(0,T),T,"+") # sums of the first n triangular numbers,
# AND the first n triangular numbers [1,3,6,2,4,7,4,6,9,7,9,12]
3 - n %in% S - n %in% T # if n is in T, it's also in S, so it's 3-2: return 1
# if n is in S but not T, it's 3-1: return 2
# if n isn't in S, it's not in T, so 3-0: return 3
}
R, (削除) 69 (削除ここまで) 58 bytes
function(n)3-n%in%(l=cumsum(1:n))-n%in%outer(c(0,l),l,"+")
Explanation:
function(n){
T <- cumsum(1:n) # first n triangular numbers [1,3,6]
S <- outer(c(0,T),T,"+") # sums of the first n triangular numbers,
# AND the first n triangular numbers [1,3,6,2,4,7,4,6,9,7,9,12]
3 - (n %in% S) - (n %in% T) # if n is in T, it's also in S, so it's 3-2: return 1
# if n is in S but not T, it's 3-1: return 2
# if n isn't in S, it's not in T, so 3-0: return 3
}
R, 69(削除) 69 (削除ここまで) 58 bytes
function(n)"if"(3-n%in%(l=cumsum(1:n)),1,"if"(-n%in%outer(lc(0,l,"+"),2l,3)"+")
cumsum(1:n) is the first n triangular numbers. If n is in l, then we return 1. outer(l,l,"+") is all sums of pairs of triangular numbers. If n is in that, then it's the sum of 2 triangular numbers. Otherwise, n must be the sum of at least 3 triangular numbers, so we return 3. Explanation:
function(n){
T <- cumsum(1:n) # first n triangular numbers [1,3,6]
S <- outer(c(0,T),T,"+") # sums of the first n triangular numbers,
# AND the first n triangular numbers [1,3,6,2,4,7,4,6,9,7,9,12]
3 - n %in% S - n %in% T # if n is in T, it's also in S, so it's 3-2: return 1
# if n is in S but not T, it's 3-1: return 2
# if n isn't in S, it's not in T, so 3-0: return 3
}
R, 69 bytes
function(n)"if"(n%in%(l=cumsum(1:n)),1,"if"(n%in%outer(l,l,"+"),2,3))
cumsum(1:n) is the first n triangular numbers. If n is in l, then we return 1. outer(l,l,"+") is all sums of pairs of triangular numbers. If n is in that, then it's the sum of 2 triangular numbers. Otherwise, n must be the sum of at least 3 triangular numbers, so we return 3.
R, (削除) 69 (削除ここまで) 58 bytes
function(n)3-n%in%(l=cumsum(1:n))-n%in%outer(c(0,l),l,"+")
Explanation:
function(n){
T <- cumsum(1:n) # first n triangular numbers [1,3,6]
S <- outer(c(0,T),T,"+") # sums of the first n triangular numbers,
# AND the first n triangular numbers [1,3,6,2,4,7,4,6,9,7,9,12]
3 - n %in% S - n %in% T # if n is in T, it's also in S, so it's 3-2: return 1
# if n is in S but not T, it's 3-1: return 2
# if n isn't in S, it's not in T, so 3-0: return 3
}
R, 69 bytes
function(n)"if"(n%in%(l=cumsum(1:n)),1,"if"(n%in%outer(l,l,"+"),2,3))
cumsum(1:n) is the first n triangular numbers. If n is in l, then we return 1. outer(l,l,"+") is all sums of pairs of triangular numbers. If n is in that, then it's the sum of 2 triangular numbers. Otherwise, n must be the sum of at least 3 triangular numbers, so we return 3.