How to test whether a number is of the form (n(n+1)(n+2))/ 6

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0












$begingroup$


Tetrahedral numbers (https://oeis.org/A000292) are:



1, 4, 10, 20, 35, 56, 84, etc



WHERE:



$$mathrmTetrahedral(n) = frac(n(n+1)(n+2))6$$



But what is the test for whether a given number is a tetrahedral number?
I also need to know, if it is a tetrahedral number, then which term in the tetrahedral sequence is it? I am looking for a formula or something that would enable me to get these results:



input result ("is tetrahedral")
1 1
2 false
3 false
4 2
5 false
6 false
7 false
8 false
9 false
10 3
11 false
12 false


I can't find any formula on the Internet for this. Any pointers in the right direction would be great.










share|cite|improve this question











$endgroup$







  • 1




    $begingroup$
    Hi & welcome to MSE. You could assume your number to test is a constant & $n$ is a variable, plug it into the equation for a tetrahedron # and solve for $n$ from the resulting $3$rd degree (i.e., cubic) polynomial.
    $endgroup$
    – John Omielan
    Jan 3 at 4:22
















0












$begingroup$


Tetrahedral numbers (https://oeis.org/A000292) are:



1, 4, 10, 20, 35, 56, 84, etc



WHERE:



$$mathrmTetrahedral(n) = frac(n(n+1)(n+2))6$$



But what is the test for whether a given number is a tetrahedral number?
I also need to know, if it is a tetrahedral number, then which term in the tetrahedral sequence is it? I am looking for a formula or something that would enable me to get these results:



input result ("is tetrahedral")
1 1
2 false
3 false
4 2
5 false
6 false
7 false
8 false
9 false
10 3
11 false
12 false


I can't find any formula on the Internet for this. Any pointers in the right direction would be great.










share|cite|improve this question











$endgroup$







  • 1




    $begingroup$
    Hi & welcome to MSE. You could assume your number to test is a constant & $n$ is a variable, plug it into the equation for a tetrahedron # and solve for $n$ from the resulting $3$rd degree (i.e., cubic) polynomial.
    $endgroup$
    – John Omielan
    Jan 3 at 4:22














0












0








0





$begingroup$


Tetrahedral numbers (https://oeis.org/A000292) are:



1, 4, 10, 20, 35, 56, 84, etc



WHERE:



$$mathrmTetrahedral(n) = frac(n(n+1)(n+2))6$$



But what is the test for whether a given number is a tetrahedral number?
I also need to know, if it is a tetrahedral number, then which term in the tetrahedral sequence is it? I am looking for a formula or something that would enable me to get these results:



input result ("is tetrahedral")
1 1
2 false
3 false
4 2
5 false
6 false
7 false
8 false
9 false
10 3
11 false
12 false


I can't find any formula on the Internet for this. Any pointers in the right direction would be great.










share|cite|improve this question











$endgroup$




Tetrahedral numbers (https://oeis.org/A000292) are:



1, 4, 10, 20, 35, 56, 84, etc



WHERE:



$$mathrmTetrahedral(n) = frac(n(n+1)(n+2))6$$



But what is the test for whether a given number is a tetrahedral number?
I also need to know, if it is a tetrahedral number, then which term in the tetrahedral sequence is it? I am looking for a formula or something that would enable me to get these results:



input result ("is tetrahedral")
1 1
2 false
3 false
4 2
5 false
6 false
7 false
8 false
9 false
10 3
11 false
12 false


I can't find any formula on the Internet for this. Any pointers in the right direction would be great.







sequences-and-series






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Jan 3 at 11:08









Mutantoe

598412




598412










asked Jan 3 at 4:14









danday74danday74

1296




1296







  • 1




    $begingroup$
    Hi & welcome to MSE. You could assume your number to test is a constant & $n$ is a variable, plug it into the equation for a tetrahedron # and solve for $n$ from the resulting $3$rd degree (i.e., cubic) polynomial.
    $endgroup$
    – John Omielan
    Jan 3 at 4:22













  • 1




    $begingroup$
    Hi & welcome to MSE. You could assume your number to test is a constant & $n$ is a variable, plug it into the equation for a tetrahedron # and solve for $n$ from the resulting $3$rd degree (i.e., cubic) polynomial.
    $endgroup$
    – John Omielan
    Jan 3 at 4:22








1




1




$begingroup$
Hi & welcome to MSE. You could assume your number to test is a constant & $n$ is a variable, plug it into the equation for a tetrahedron # and solve for $n$ from the resulting $3$rd degree (i.e., cubic) polynomial.
$endgroup$
– John Omielan
Jan 3 at 4:22





$begingroup$
Hi & welcome to MSE. You could assume your number to test is a constant & $n$ is a variable, plug it into the equation for a tetrahedron # and solve for $n$ from the resulting $3$rd degree (i.e., cubic) polynomial.
$endgroup$
– John Omielan
Jan 3 at 4:22











4 Answers
4






active

oldest

votes


















4












$begingroup$

Another way to look at that: $fracn(n+1)(n+2)6=frac(n+1)^3-(n+1)6$.



So then, if we have some $m$ we want to test? Multiply it by $6$. Add its cube root, rounded up - $k^3-k > (k-1)^3$ for all $k > 1$. If we get a perfect cube ($6m+lceilsqrt[3]6mrceil=k^3$ for some integer $k$), it's a tetrahedral number (Specifically, for $n=k-1$). If not, it isn't.






share|cite|improve this answer









$endgroup$












  • $begingroup$
    implemented, works well in all test cases thx, but only returns a boolean (or am I doing something wrong?) - still very useful +1
    $endgroup$
    – danday74
    Jan 4 at 1:37







  • 1




    $begingroup$
    So, I take it you didn't implement the part in the parenthetical comment that says which one it is?
    $endgroup$
    – jmerry
    Jan 4 at 3:26










  • $begingroup$
    with you now, I kinda follow what's going on - soooo clever! many thx - switching to this approach shaved 100ms off my code execution time
    $endgroup$
    – danday74
    Jan 4 at 4:12


















3












$begingroup$

Notice that $n(n+1)(n+2)/6approx n^3/6$ for large values of $n$. Thus, if you have a suspected tetrahedral number $k$, and you compute $n_0=lfloorsqrt[3]6krfloor$ you will have a pretty good guess as to which index you need to check to confirm or refute your suspicions.



This of course won't work for small $k$, and your initial guess $n_0$ need not be correct, but you have at least vastly reduced the number of operations you need to finish your problem. The full algorithm now would look something like this:



1) Take the input number $k$ and compute $n_0=lfloorsqrt[3]6krfloor$.



2) If $textTetra(n_0)>k$, begin testing all naturals $n<n_0$ (in decreasing order) until $textTetra(n)leq k$. If you achieve equality, then $k$ is tetrahedral with index $n$. If you don't have equality, then $k$ is not tetrahedral.



3) If $textTetra(n_0)<k$, begin testing all naturals $n>n_0$ until $textTetra(n)geq k$. If you achieve equality, then $k$ is tetrahedral with index $n$. If you don't have equality, then $k$ is not tetrahedral.



4) If $textTetra(n_0)=k$, then you are done, and the index is $n_0$.






share|cite|improve this answer









$endgroup$












  • $begingroup$
    nice answer thx, works in all cases, very few iterations needed
    $endgroup$
    – danday74
    Jan 4 at 2:27










  • $begingroup$
    other guy pipped you to post, his works without any iterations - but a superb answer, many thx - and a great generic approach to reducing iterations
    $endgroup$
    – danday74
    Jan 4 at 4:02


















3












$begingroup$

For a given $k$, you want to know if it exists an integer $n$ such that
$$fracn(n+1)(n+2)6=k$$ A simple way is to look at the cubic equation
$$n^3+3n^2+2n-6k=0$$ which has only one real root. Using the hyperbolic solution for this case, the solution is given by
$$n=-1+frac2 sqrt3cosh left(frac13 cosh ^-1left(9 sqrt3,
kright)right)$$
If this $n$ is an integer (or very very close to because of possible rounding errors), then you are done.



If you prefer "simpler" analytical formulae, you also have
$$n=-1+t_1+t_2$$ where
$$t_1^3=frac13 left(sqrt3 sqrt243 k^2-1+27 kright)qquad textand qquad t_2^3=frac19 left(sqrt3 sqrt243 k^2-1+27 kright)$$






share|cite|improve this answer











$endgroup$












  • $begingroup$
    really appreciate your help, accepted other answer on the basis that usually only 2 iterations are needed and that this MAY be computationally more expensive - but I haven't tested performance and this MAY be faster - indeed, where performance is not an issue, this is undoubtedly a great answer
    $endgroup$
    – danday74
    Jan 4 at 3:34


















1












$begingroup$

Assume that $n(n+1)(n+2)/6=k$. Then $n(n+1)(n+2)-6k=0$, which has exactly one positive root for any positive $k$ (the left side increases monotonically and without bound from a negative intercept for positive $n$). If this is to be an integer then the rational root theorem guarantees that it will be a divisor of $6k$. So test such divisors in increasing order until you hit $n(n+1)(n+2)=6k$ or overshoot with $n(n+1)(n+2)>6k$.



We can cut down on trials by using the property that a tetrahedral number us the sum of alternating squares. With an odd argument the sum is $1^2+3^2+5^2+...$ with the sum truncated at the argument, thus the fifth nonzero terrahedral number is $1^2+3^2+5^2$. With an even argument we would use $2^2+4^2+6^2+...$.



If the argument $n$ is odd then, because all odd squares are $equiv 1bmod 8$, we must have $nequiv 2k-1bmod 8$. An even $n$ gives squares alternating between $4$ and $0bmod 8$, so if $k$ is divisible by $8$ then $nin0,6bmod 8$. If $k$ is instead four times an odd number then then $nin2,4bmod 8$.



Let's apply these ideas to $84$. This is four times an odd number, so an odd argument must be $equiv 7bmod 8$ and an even argument must be $in2,4bmod 8$. The divisors of $6×84=504$ satisfying these modular requirements are



$2,4,7,12,18,...,252$



We begin testing these in ascending order. We might see that $(8)(8+1)(8+2)>8^3>504$ (the cube root test in another answer), so either $2,4$ or $7$ works or else nothing does.






share|cite|improve this answer











$endgroup$












    Your Answer





    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "69"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3060249%2fhow-to-test-whether-a-number-is-of-the-form-nn1n2-6%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4












    $begingroup$

    Another way to look at that: $fracn(n+1)(n+2)6=frac(n+1)^3-(n+1)6$.



    So then, if we have some $m$ we want to test? Multiply it by $6$. Add its cube root, rounded up - $k^3-k > (k-1)^3$ for all $k > 1$. If we get a perfect cube ($6m+lceilsqrt[3]6mrceil=k^3$ for some integer $k$), it's a tetrahedral number (Specifically, for $n=k-1$). If not, it isn't.






    share|cite|improve this answer









    $endgroup$












    • $begingroup$
      implemented, works well in all test cases thx, but only returns a boolean (or am I doing something wrong?) - still very useful +1
      $endgroup$
      – danday74
      Jan 4 at 1:37







    • 1




      $begingroup$
      So, I take it you didn't implement the part in the parenthetical comment that says which one it is?
      $endgroup$
      – jmerry
      Jan 4 at 3:26










    • $begingroup$
      with you now, I kinda follow what's going on - soooo clever! many thx - switching to this approach shaved 100ms off my code execution time
      $endgroup$
      – danday74
      Jan 4 at 4:12















    4












    $begingroup$

    Another way to look at that: $fracn(n+1)(n+2)6=frac(n+1)^3-(n+1)6$.



    So then, if we have some $m$ we want to test? Multiply it by $6$. Add its cube root, rounded up - $k^3-k > (k-1)^3$ for all $k > 1$. If we get a perfect cube ($6m+lceilsqrt[3]6mrceil=k^3$ for some integer $k$), it's a tetrahedral number (Specifically, for $n=k-1$). If not, it isn't.






    share|cite|improve this answer









    $endgroup$












    • $begingroup$
      implemented, works well in all test cases thx, but only returns a boolean (or am I doing something wrong?) - still very useful +1
      $endgroup$
      – danday74
      Jan 4 at 1:37







    • 1




      $begingroup$
      So, I take it you didn't implement the part in the parenthetical comment that says which one it is?
      $endgroup$
      – jmerry
      Jan 4 at 3:26










    • $begingroup$
      with you now, I kinda follow what's going on - soooo clever! many thx - switching to this approach shaved 100ms off my code execution time
      $endgroup$
      – danday74
      Jan 4 at 4:12













    4












    4








    4





    $begingroup$

    Another way to look at that: $fracn(n+1)(n+2)6=frac(n+1)^3-(n+1)6$.



    So then, if we have some $m$ we want to test? Multiply it by $6$. Add its cube root, rounded up - $k^3-k > (k-1)^3$ for all $k > 1$. If we get a perfect cube ($6m+lceilsqrt[3]6mrceil=k^3$ for some integer $k$), it's a tetrahedral number (Specifically, for $n=k-1$). If not, it isn't.






    share|cite|improve this answer









    $endgroup$



    Another way to look at that: $fracn(n+1)(n+2)6=frac(n+1)^3-(n+1)6$.



    So then, if we have some $m$ we want to test? Multiply it by $6$. Add its cube root, rounded up - $k^3-k > (k-1)^3$ for all $k > 1$. If we get a perfect cube ($6m+lceilsqrt[3]6mrceil=k^3$ for some integer $k$), it's a tetrahedral number (Specifically, for $n=k-1$). If not, it isn't.







    share|cite|improve this answer












    share|cite|improve this answer



    share|cite|improve this answer










    answered Jan 3 at 4:28









    jmerryjmerry

    3,792514




    3,792514











    • $begingroup$
      implemented, works well in all test cases thx, but only returns a boolean (or am I doing something wrong?) - still very useful +1
      $endgroup$
      – danday74
      Jan 4 at 1:37







    • 1




      $begingroup$
      So, I take it you didn't implement the part in the parenthetical comment that says which one it is?
      $endgroup$
      – jmerry
      Jan 4 at 3:26










    • $begingroup$
      with you now, I kinda follow what's going on - soooo clever! many thx - switching to this approach shaved 100ms off my code execution time
      $endgroup$
      – danday74
      Jan 4 at 4:12
















    • $begingroup$
      implemented, works well in all test cases thx, but only returns a boolean (or am I doing something wrong?) - still very useful +1
      $endgroup$
      – danday74
      Jan 4 at 1:37







    • 1




      $begingroup$
      So, I take it you didn't implement the part in the parenthetical comment that says which one it is?
      $endgroup$
      – jmerry
      Jan 4 at 3:26










    • $begingroup$
      with you now, I kinda follow what's going on - soooo clever! many thx - switching to this approach shaved 100ms off my code execution time
      $endgroup$
      – danday74
      Jan 4 at 4:12















    $begingroup$
    implemented, works well in all test cases thx, but only returns a boolean (or am I doing something wrong?) - still very useful +1
    $endgroup$
    – danday74
    Jan 4 at 1:37





    $begingroup$
    implemented, works well in all test cases thx, but only returns a boolean (or am I doing something wrong?) - still very useful +1
    $endgroup$
    – danday74
    Jan 4 at 1:37





    1




    1




    $begingroup$
    So, I take it you didn't implement the part in the parenthetical comment that says which one it is?
    $endgroup$
    – jmerry
    Jan 4 at 3:26




    $begingroup$
    So, I take it you didn't implement the part in the parenthetical comment that says which one it is?
    $endgroup$
    – jmerry
    Jan 4 at 3:26












    $begingroup$
    with you now, I kinda follow what's going on - soooo clever! many thx - switching to this approach shaved 100ms off my code execution time
    $endgroup$
    – danday74
    Jan 4 at 4:12




    $begingroup$
    with you now, I kinda follow what's going on - soooo clever! many thx - switching to this approach shaved 100ms off my code execution time
    $endgroup$
    – danday74
    Jan 4 at 4:12











    3












    $begingroup$

    Notice that $n(n+1)(n+2)/6approx n^3/6$ for large values of $n$. Thus, if you have a suspected tetrahedral number $k$, and you compute $n_0=lfloorsqrt[3]6krfloor$ you will have a pretty good guess as to which index you need to check to confirm or refute your suspicions.



    This of course won't work for small $k$, and your initial guess $n_0$ need not be correct, but you have at least vastly reduced the number of operations you need to finish your problem. The full algorithm now would look something like this:



    1) Take the input number $k$ and compute $n_0=lfloorsqrt[3]6krfloor$.



    2) If $textTetra(n_0)>k$, begin testing all naturals $n<n_0$ (in decreasing order) until $textTetra(n)leq k$. If you achieve equality, then $k$ is tetrahedral with index $n$. If you don't have equality, then $k$ is not tetrahedral.



    3) If $textTetra(n_0)<k$, begin testing all naturals $n>n_0$ until $textTetra(n)geq k$. If you achieve equality, then $k$ is tetrahedral with index $n$. If you don't have equality, then $k$ is not tetrahedral.



    4) If $textTetra(n_0)=k$, then you are done, and the index is $n_0$.






    share|cite|improve this answer









    $endgroup$












    • $begingroup$
      nice answer thx, works in all cases, very few iterations needed
      $endgroup$
      – danday74
      Jan 4 at 2:27










    • $begingroup$
      other guy pipped you to post, his works without any iterations - but a superb answer, many thx - and a great generic approach to reducing iterations
      $endgroup$
      – danday74
      Jan 4 at 4:02















    3












    $begingroup$

    Notice that $n(n+1)(n+2)/6approx n^3/6$ for large values of $n$. Thus, if you have a suspected tetrahedral number $k$, and you compute $n_0=lfloorsqrt[3]6krfloor$ you will have a pretty good guess as to which index you need to check to confirm or refute your suspicions.



    This of course won't work for small $k$, and your initial guess $n_0$ need not be correct, but you have at least vastly reduced the number of operations you need to finish your problem. The full algorithm now would look something like this:



    1) Take the input number $k$ and compute $n_0=lfloorsqrt[3]6krfloor$.



    2) If $textTetra(n_0)>k$, begin testing all naturals $n<n_0$ (in decreasing order) until $textTetra(n)leq k$. If you achieve equality, then $k$ is tetrahedral with index $n$. If you don't have equality, then $k$ is not tetrahedral.



    3) If $textTetra(n_0)<k$, begin testing all naturals $n>n_0$ until $textTetra(n)geq k$. If you achieve equality, then $k$ is tetrahedral with index $n$. If you don't have equality, then $k$ is not tetrahedral.



    4) If $textTetra(n_0)=k$, then you are done, and the index is $n_0$.






    share|cite|improve this answer









    $endgroup$












    • $begingroup$
      nice answer thx, works in all cases, very few iterations needed
      $endgroup$
      – danday74
      Jan 4 at 2:27










    • $begingroup$
      other guy pipped you to post, his works without any iterations - but a superb answer, many thx - and a great generic approach to reducing iterations
      $endgroup$
      – danday74
      Jan 4 at 4:02













    3












    3








    3





    $begingroup$

    Notice that $n(n+1)(n+2)/6approx n^3/6$ for large values of $n$. Thus, if you have a suspected tetrahedral number $k$, and you compute $n_0=lfloorsqrt[3]6krfloor$ you will have a pretty good guess as to which index you need to check to confirm or refute your suspicions.



    This of course won't work for small $k$, and your initial guess $n_0$ need not be correct, but you have at least vastly reduced the number of operations you need to finish your problem. The full algorithm now would look something like this:



    1) Take the input number $k$ and compute $n_0=lfloorsqrt[3]6krfloor$.



    2) If $textTetra(n_0)>k$, begin testing all naturals $n<n_0$ (in decreasing order) until $textTetra(n)leq k$. If you achieve equality, then $k$ is tetrahedral with index $n$. If you don't have equality, then $k$ is not tetrahedral.



    3) If $textTetra(n_0)<k$, begin testing all naturals $n>n_0$ until $textTetra(n)geq k$. If you achieve equality, then $k$ is tetrahedral with index $n$. If you don't have equality, then $k$ is not tetrahedral.



    4) If $textTetra(n_0)=k$, then you are done, and the index is $n_0$.






    share|cite|improve this answer









    $endgroup$



    Notice that $n(n+1)(n+2)/6approx n^3/6$ for large values of $n$. Thus, if you have a suspected tetrahedral number $k$, and you compute $n_0=lfloorsqrt[3]6krfloor$ you will have a pretty good guess as to which index you need to check to confirm or refute your suspicions.



    This of course won't work for small $k$, and your initial guess $n_0$ need not be correct, but you have at least vastly reduced the number of operations you need to finish your problem. The full algorithm now would look something like this:



    1) Take the input number $k$ and compute $n_0=lfloorsqrt[3]6krfloor$.



    2) If $textTetra(n_0)>k$, begin testing all naturals $n<n_0$ (in decreasing order) until $textTetra(n)leq k$. If you achieve equality, then $k$ is tetrahedral with index $n$. If you don't have equality, then $k$ is not tetrahedral.



    3) If $textTetra(n_0)<k$, begin testing all naturals $n>n_0$ until $textTetra(n)geq k$. If you achieve equality, then $k$ is tetrahedral with index $n$. If you don't have equality, then $k$ is not tetrahedral.



    4) If $textTetra(n_0)=k$, then you are done, and the index is $n_0$.







    share|cite|improve this answer












    share|cite|improve this answer



    share|cite|improve this answer










    answered Jan 3 at 4:27









    ItsJustASeriesBroItsJustASeriesBro

    1563




    1563











    • $begingroup$
      nice answer thx, works in all cases, very few iterations needed
      $endgroup$
      – danday74
      Jan 4 at 2:27










    • $begingroup$
      other guy pipped you to post, his works without any iterations - but a superb answer, many thx - and a great generic approach to reducing iterations
      $endgroup$
      – danday74
      Jan 4 at 4:02
















    • $begingroup$
      nice answer thx, works in all cases, very few iterations needed
      $endgroup$
      – danday74
      Jan 4 at 2:27










    • $begingroup$
      other guy pipped you to post, his works without any iterations - but a superb answer, many thx - and a great generic approach to reducing iterations
      $endgroup$
      – danday74
      Jan 4 at 4:02















    $begingroup$
    nice answer thx, works in all cases, very few iterations needed
    $endgroup$
    – danday74
    Jan 4 at 2:27




    $begingroup$
    nice answer thx, works in all cases, very few iterations needed
    $endgroup$
    – danday74
    Jan 4 at 2:27












    $begingroup$
    other guy pipped you to post, his works without any iterations - but a superb answer, many thx - and a great generic approach to reducing iterations
    $endgroup$
    – danday74
    Jan 4 at 4:02




    $begingroup$
    other guy pipped you to post, his works without any iterations - but a superb answer, many thx - and a great generic approach to reducing iterations
    $endgroup$
    – danday74
    Jan 4 at 4:02











    3












    $begingroup$

    For a given $k$, you want to know if it exists an integer $n$ such that
    $$fracn(n+1)(n+2)6=k$$ A simple way is to look at the cubic equation
    $$n^3+3n^2+2n-6k=0$$ which has only one real root. Using the hyperbolic solution for this case, the solution is given by
    $$n=-1+frac2 sqrt3cosh left(frac13 cosh ^-1left(9 sqrt3,
    kright)right)$$
    If this $n$ is an integer (or very very close to because of possible rounding errors), then you are done.



    If you prefer "simpler" analytical formulae, you also have
    $$n=-1+t_1+t_2$$ where
    $$t_1^3=frac13 left(sqrt3 sqrt243 k^2-1+27 kright)qquad textand qquad t_2^3=frac19 left(sqrt3 sqrt243 k^2-1+27 kright)$$






    share|cite|improve this answer











    $endgroup$












    • $begingroup$
      really appreciate your help, accepted other answer on the basis that usually only 2 iterations are needed and that this MAY be computationally more expensive - but I haven't tested performance and this MAY be faster - indeed, where performance is not an issue, this is undoubtedly a great answer
      $endgroup$
      – danday74
      Jan 4 at 3:34















    3












    $begingroup$

    For a given $k$, you want to know if it exists an integer $n$ such that
    $$fracn(n+1)(n+2)6=k$$ A simple way is to look at the cubic equation
    $$n^3+3n^2+2n-6k=0$$ which has only one real root. Using the hyperbolic solution for this case, the solution is given by
    $$n=-1+frac2 sqrt3cosh left(frac13 cosh ^-1left(9 sqrt3,
    kright)right)$$
    If this $n$ is an integer (or very very close to because of possible rounding errors), then you are done.



    If you prefer "simpler" analytical formulae, you also have
    $$n=-1+t_1+t_2$$ where
    $$t_1^3=frac13 left(sqrt3 sqrt243 k^2-1+27 kright)qquad textand qquad t_2^3=frac19 left(sqrt3 sqrt243 k^2-1+27 kright)$$






    share|cite|improve this answer











    $endgroup$












    • $begingroup$
      really appreciate your help, accepted other answer on the basis that usually only 2 iterations are needed and that this MAY be computationally more expensive - but I haven't tested performance and this MAY be faster - indeed, where performance is not an issue, this is undoubtedly a great answer
      $endgroup$
      – danday74
      Jan 4 at 3:34













    3












    3








    3





    $begingroup$

    For a given $k$, you want to know if it exists an integer $n$ such that
    $$fracn(n+1)(n+2)6=k$$ A simple way is to look at the cubic equation
    $$n^3+3n^2+2n-6k=0$$ which has only one real root. Using the hyperbolic solution for this case, the solution is given by
    $$n=-1+frac2 sqrt3cosh left(frac13 cosh ^-1left(9 sqrt3,
    kright)right)$$
    If this $n$ is an integer (or very very close to because of possible rounding errors), then you are done.



    If you prefer "simpler" analytical formulae, you also have
    $$n=-1+t_1+t_2$$ where
    $$t_1^3=frac13 left(sqrt3 sqrt243 k^2-1+27 kright)qquad textand qquad t_2^3=frac19 left(sqrt3 sqrt243 k^2-1+27 kright)$$






    share|cite|improve this answer











    $endgroup$



    For a given $k$, you want to know if it exists an integer $n$ such that
    $$fracn(n+1)(n+2)6=k$$ A simple way is to look at the cubic equation
    $$n^3+3n^2+2n-6k=0$$ which has only one real root. Using the hyperbolic solution for this case, the solution is given by
    $$n=-1+frac2 sqrt3cosh left(frac13 cosh ^-1left(9 sqrt3,
    kright)right)$$
    If this $n$ is an integer (or very very close to because of possible rounding errors), then you are done.



    If you prefer "simpler" analytical formulae, you also have
    $$n=-1+t_1+t_2$$ where
    $$t_1^3=frac13 left(sqrt3 sqrt243 k^2-1+27 kright)qquad textand qquad t_2^3=frac19 left(sqrt3 sqrt243 k^2-1+27 kright)$$







    share|cite|improve this answer














    share|cite|improve this answer



    share|cite|improve this answer








    edited Jan 3 at 7:21

























    answered Jan 3 at 6:39









    Claude LeiboviciClaude Leibovici

    120k1157132




    120k1157132











    • $begingroup$
      really appreciate your help, accepted other answer on the basis that usually only 2 iterations are needed and that this MAY be computationally more expensive - but I haven't tested performance and this MAY be faster - indeed, where performance is not an issue, this is undoubtedly a great answer
      $endgroup$
      – danday74
      Jan 4 at 3:34
















    • $begingroup$
      really appreciate your help, accepted other answer on the basis that usually only 2 iterations are needed and that this MAY be computationally more expensive - but I haven't tested performance and this MAY be faster - indeed, where performance is not an issue, this is undoubtedly a great answer
      $endgroup$
      – danday74
      Jan 4 at 3:34















    $begingroup$
    really appreciate your help, accepted other answer on the basis that usually only 2 iterations are needed and that this MAY be computationally more expensive - but I haven't tested performance and this MAY be faster - indeed, where performance is not an issue, this is undoubtedly a great answer
    $endgroup$
    – danday74
    Jan 4 at 3:34




    $begingroup$
    really appreciate your help, accepted other answer on the basis that usually only 2 iterations are needed and that this MAY be computationally more expensive - but I haven't tested performance and this MAY be faster - indeed, where performance is not an issue, this is undoubtedly a great answer
    $endgroup$
    – danday74
    Jan 4 at 3:34











    1












    $begingroup$

    Assume that $n(n+1)(n+2)/6=k$. Then $n(n+1)(n+2)-6k=0$, which has exactly one positive root for any positive $k$ (the left side increases monotonically and without bound from a negative intercept for positive $n$). If this is to be an integer then the rational root theorem guarantees that it will be a divisor of $6k$. So test such divisors in increasing order until you hit $n(n+1)(n+2)=6k$ or overshoot with $n(n+1)(n+2)>6k$.



    We can cut down on trials by using the property that a tetrahedral number us the sum of alternating squares. With an odd argument the sum is $1^2+3^2+5^2+...$ with the sum truncated at the argument, thus the fifth nonzero terrahedral number is $1^2+3^2+5^2$. With an even argument we would use $2^2+4^2+6^2+...$.



    If the argument $n$ is odd then, because all odd squares are $equiv 1bmod 8$, we must have $nequiv 2k-1bmod 8$. An even $n$ gives squares alternating between $4$ and $0bmod 8$, so if $k$ is divisible by $8$ then $nin0,6bmod 8$. If $k$ is instead four times an odd number then then $nin2,4bmod 8$.



    Let's apply these ideas to $84$. This is four times an odd number, so an odd argument must be $equiv 7bmod 8$ and an even argument must be $in2,4bmod 8$. The divisors of $6×84=504$ satisfying these modular requirements are



    $2,4,7,12,18,...,252$



    We begin testing these in ascending order. We might see that $(8)(8+1)(8+2)>8^3>504$ (the cube root test in another answer), so either $2,4$ or $7$ works or else nothing does.






    share|cite|improve this answer











    $endgroup$

















      1












      $begingroup$

      Assume that $n(n+1)(n+2)/6=k$. Then $n(n+1)(n+2)-6k=0$, which has exactly one positive root for any positive $k$ (the left side increases monotonically and without bound from a negative intercept for positive $n$). If this is to be an integer then the rational root theorem guarantees that it will be a divisor of $6k$. So test such divisors in increasing order until you hit $n(n+1)(n+2)=6k$ or overshoot with $n(n+1)(n+2)>6k$.



      We can cut down on trials by using the property that a tetrahedral number us the sum of alternating squares. With an odd argument the sum is $1^2+3^2+5^2+...$ with the sum truncated at the argument, thus the fifth nonzero terrahedral number is $1^2+3^2+5^2$. With an even argument we would use $2^2+4^2+6^2+...$.



      If the argument $n$ is odd then, because all odd squares are $equiv 1bmod 8$, we must have $nequiv 2k-1bmod 8$. An even $n$ gives squares alternating between $4$ and $0bmod 8$, so if $k$ is divisible by $8$ then $nin0,6bmod 8$. If $k$ is instead four times an odd number then then $nin2,4bmod 8$.



      Let's apply these ideas to $84$. This is four times an odd number, so an odd argument must be $equiv 7bmod 8$ and an even argument must be $in2,4bmod 8$. The divisors of $6×84=504$ satisfying these modular requirements are



      $2,4,7,12,18,...,252$



      We begin testing these in ascending order. We might see that $(8)(8+1)(8+2)>8^3>504$ (the cube root test in another answer), so either $2,4$ or $7$ works or else nothing does.






      share|cite|improve this answer











      $endgroup$















        1












        1








        1





        $begingroup$

        Assume that $n(n+1)(n+2)/6=k$. Then $n(n+1)(n+2)-6k=0$, which has exactly one positive root for any positive $k$ (the left side increases monotonically and without bound from a negative intercept for positive $n$). If this is to be an integer then the rational root theorem guarantees that it will be a divisor of $6k$. So test such divisors in increasing order until you hit $n(n+1)(n+2)=6k$ or overshoot with $n(n+1)(n+2)>6k$.



        We can cut down on trials by using the property that a tetrahedral number us the sum of alternating squares. With an odd argument the sum is $1^2+3^2+5^2+...$ with the sum truncated at the argument, thus the fifth nonzero terrahedral number is $1^2+3^2+5^2$. With an even argument we would use $2^2+4^2+6^2+...$.



        If the argument $n$ is odd then, because all odd squares are $equiv 1bmod 8$, we must have $nequiv 2k-1bmod 8$. An even $n$ gives squares alternating between $4$ and $0bmod 8$, so if $k$ is divisible by $8$ then $nin0,6bmod 8$. If $k$ is instead four times an odd number then then $nin2,4bmod 8$.



        Let's apply these ideas to $84$. This is four times an odd number, so an odd argument must be $equiv 7bmod 8$ and an even argument must be $in2,4bmod 8$. The divisors of $6×84=504$ satisfying these modular requirements are



        $2,4,7,12,18,...,252$



        We begin testing these in ascending order. We might see that $(8)(8+1)(8+2)>8^3>504$ (the cube root test in another answer), so either $2,4$ or $7$ works or else nothing does.






        share|cite|improve this answer











        $endgroup$



        Assume that $n(n+1)(n+2)/6=k$. Then $n(n+1)(n+2)-6k=0$, which has exactly one positive root for any positive $k$ (the left side increases monotonically and without bound from a negative intercept for positive $n$). If this is to be an integer then the rational root theorem guarantees that it will be a divisor of $6k$. So test such divisors in increasing order until you hit $n(n+1)(n+2)=6k$ or overshoot with $n(n+1)(n+2)>6k$.



        We can cut down on trials by using the property that a tetrahedral number us the sum of alternating squares. With an odd argument the sum is $1^2+3^2+5^2+...$ with the sum truncated at the argument, thus the fifth nonzero terrahedral number is $1^2+3^2+5^2$. With an even argument we would use $2^2+4^2+6^2+...$.



        If the argument $n$ is odd then, because all odd squares are $equiv 1bmod 8$, we must have $nequiv 2k-1bmod 8$. An even $n$ gives squares alternating between $4$ and $0bmod 8$, so if $k$ is divisible by $8$ then $nin0,6bmod 8$. If $k$ is instead four times an odd number then then $nin2,4bmod 8$.



        Let's apply these ideas to $84$. This is four times an odd number, so an odd argument must be $equiv 7bmod 8$ and an even argument must be $in2,4bmod 8$. The divisors of $6×84=504$ satisfying these modular requirements are



        $2,4,7,12,18,...,252$



        We begin testing these in ascending order. We might see that $(8)(8+1)(8+2)>8^3>504$ (the cube root test in another answer), so either $2,4$ or $7$ works or else nothing does.







        share|cite|improve this answer














        share|cite|improve this answer



        share|cite|improve this answer








        edited Jan 3 at 14:46

























        answered Jan 3 at 12:59









        Oscar LanziOscar Lanzi

        12.3k12036




        12.3k12036



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Mathematics Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3060249%2fhow-to-test-whether-a-number-is-of-the-form-nn1n2-6%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?