Patterns and Sequence - Express 'a' as a function of 'n'

Patterns and Sequence - Express 'a' as a function of 'n'



I have the following pattern. I want to express 'a' as a function of n.


if n=0 then a=0
if n=1 then a=0
if n=2 then a=3
if n=3 then a=3
if n=4 then a=10
.
.
.
if n=10 then a=10
if n=11 then a=29
.
.
.
if n=29 then a=29
if n=30 then a=66
.
.
.
if n=66 then a=66
if n=67 then a=127
.
.



AS you can see the value of a remains the same till a value matches with n. After which the value of a changes and again this value holds till a<=n. I found the formula with which this pattern occurrs. It is


a


a


n


a


a<=n



a = 1^3 + 2 when n<=3


a = 1^3 + 2


n<=3



a = 2^3 + 2 when n > 3 and n <=10
and so on.


a = 2^3 + 2


n > 3 and n <=10



How to express a as a function of n ?
like f(n) = {___ <condition>


a


n


f(n) = {___ <condition>





what is the value of a for n > 10 ? it is again 1^3 + 2 ?
– zenwraight
Sep 4 '18 at 21:00





@zenwraight for n > 10 and n <= 29 a = 3^3 + 2 ,, n>29 and n <= 66 a=4^3 + 2
– Maha
Sep 4 '18 at 21:06



n > 10


n <= 29


a = 3^3 + 2


n>29


n <= 66


a=4^3 + 2





"AS you can see the value of a remains the same till a value matches with n" contradicts to first four examples for n and a you give. Or did I miss something ?
– rocksportrocker
Sep 4 '18 at 21:15





Why does the sequence start with 0 instead of 2?
– m69
Sep 4 '18 at 21:17





@rocksportrocker Yes it contradicts for n=0 and n=1. So i am assuming those are base cases. The pattern starts from n = 2. a=1^3 + 2. so a = 3. when n=3, a=3. when n=4, a=2^3+2=10.Then for n=4,5,6,7,8,9,10, the a=10. When n=11, a=3^3+2=29. Then for n=11 till 29 the a=29
– Maha
Sep 4 '18 at 21:24




1 Answer
1



You can apply the inverse of the formula n^3-2, round up, and then apply the formula again, to get the correct sequence. The values for 0, 1 and 2 have to be hard-coded, though.


n^3-2



Note: in languages with typed numbers, make sure the result of the cubic root is a float; if it is converted to int automatically, it will be rounded down in the conversion.




function calculate(n)
if (n <= 1) return 0;
if (n == 2) return 3;
return Math.pow(Math.ceil(Math.pow(n - 2, 1 / 3)), 3) + 2;


for (var i = 0; i < 70; i++)
document.write(i + "&rarr;" + calculate(i) + " ; ");



Addendum: as Stefan Mondelaers commented, you have to be careful when relying on floating point maths. The code above makes use of the fact that cubic roots of third powers are always slightly underestimated in JavaScript (at least in all current browsers I tested); e.g. the largest third power within JavaScript's safe integer range is 4,503,569,204,744,000 but instead of its cubic root 165,140 you will get:




document.write(Math.pow(4503569204744000, 1/3));



If you're going to round off the results of floating point calculations, these very small errors may lead to bigger errors. The simplest work-around is indeed to add or subtract a very small value before rounding. For more information see e.g. this question.





Amazing ! Thank you so much !
– Maha
Sep 4 '18 at 22:38





Looks good but the Math.ceil fuction is a discrete function and might cause unexpected results for some values because of rounding errors in the floating point calculation of its argument. Even with a high precision floating point, the smallest possible rounding error because of the limited precision of the floating point can have effect on the Math.ceil method if the argument would be an integer if exactly calculated. To solve that problem, I would subtract a very small number from the result of Math.pow before applying Math.ceil.
– Stefan Mondelaers
Sep 21 '18 at 12:30





@StefanMondelaers In JavaScript this is safe, because Math.pow(Math.pow(n,3),1/3) is consistently less than or equal to n, but you're right that you can't really count on that to always be true. I'll add a note to the answer.
– m69
Sep 21 '18 at 23:31


Math.pow(Math.pow(n,3),1/3)



Thanks for contributing an answer to Stack Overflow!



But avoid



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



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



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



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)