+ 16
Can you help me to print this pattern? 1,2,6,15,31
8 Answers
+ 19
● using loop
for(int i=0,a=1;i<5;i++)
System.out.println(a+=i*i);
● we can do it in 1 liner recursion also (easy one , give it a try)
//same loop can be used in all languages mentioned ☺👍
+ 16
thanks Микола Федосєєв
+ 13
Thanks Kishalaya Saha i hv just coded it now😊😊
+ 11
thanks Gaurav Agrawal 😊
+ 9
Here's the pattern: if we assume a[n] represents the terms of the sequence for n=0,1, 2, then
a[0] = 1
and for n>0,
a[n] = a[n-1] + n**2
Can you code it now? If you get stuck, show your attempt, and we'd be happy to help you out.
+ 4
а[n] = (n(n+1)(2n+1))/6 +1
+ 2
Well done, and you're welcome Crystal!! 😊