Another alternative way of doing it is arithmetic series:
where n = int(b/2-1) and ![alt text][2] and A1 = 1 and An = n
Example: B = 20, n =(20/2-1) = 9, 9(1+9)/2 = 45
Another alternative way of doing it is arithmetic series:
where n = int(b/2-1) and ![alt text][2] and A1 = 1 and An = n
Example: B = 20, n =(20/2-1) = 9, 9(1+9)/2 = 45
Recursive function for this problem is f(x) = 1 + 2*f(x-2) - f(x-4)
F(B)=B/2+(B/2-1)+(B/2-2)+…+1
can anyone plz tell me from where the 1st B/2 term come?
@subarna_08.
Correct summation is : ((B/2) - 1) + ((B/2) - 2) + … + 1 = Sum of first ((B/2) - 1) positive integers.
Hence the correct answer is: ( ((B/2) - 1)(B/2) )/2.
(Sum of first n positive integers is n(n+1)/2)
Can anyone tell me why the code with precompute is throwing the error NZEC, while without precompute it is working correctly in Java?
Code:
With Precompute: CodeChef: Practical coding for everyone
WithOUT precompute: CodeChef: Practical coding for everyone
Java Recursive solution:
import java.util.*;
class Solution
{
public static int derive(int n)
{
if(n==1 ||n==2 ||n==3)
return 0;
return (n/2-1)+derive(n-2);
}
public static void main(String []args)
{
Scanner sc=new Scanner (System.in);
int item=sc.nextInt();
while(item!=0)
{
item--;
int n=sc.nextInt();
int x=derive(n);
System.out.println(x);
}
}
}
“we will have (B/2−1) squares in the base, and we will be left with another isosceles right angle triangle having base length (B−2).”
Where does this thing come from… please help me …
correction:
Hey!. Let the base length is B. Since the triangle is right-angled isosceles, even the perpendicular is B. The angle between base and hypotenuse is 45 degrees.(tan theta = B/B = 1). Now we need to find the position on the base beyond which a square of 2 units cannot exist. Again from tan theta, this is 2. Therefore, squares can be drawn on B-2 space. Therefore number of squares = (B-2)/2 = B/2 - 1. These squares are on the base. The part left in perpendicular is B-2, which is again a right-angled isosceles triangle. Hope this helps!
Here is the recursive code (Python 3.6) :
def rec(b):
if b<4:
return 0
else:
return (b//2 - 1) + rec(b-2)
if __name__==‘__main__’:
for _ in range(int(input())):
print(rec(int(input())))
Here is the optimal code:
for _ in range(int(input())):
b = int(input())
if b<4:
print(0)
continue
else:
if b%2==1:
b-=1
print(((b-2)*(b))//8)
Can you please help me with this, I am getting NZEC for the code,
def recursion(n):
if(n<4):
return 0
else:
return recursion(n-2)+(n//2)-1
for t in range(int(input())):
n = int(input())
print(recursion(n))
Why am I getting NZEC for this code?
def recursion(n):
if(n<4):
return 0
else:
return recursion(n-2)+(n//2)-1
for t in range(int(input())):
n = int(input())
print(recursion(n))
If n=10**4 (see constraints) then it will raise recursion error hence it is better to use mathematical approach than recursion one. Hope this helps. Happy coding!
This solution is for a square of nxn.
Triangle base is B.
int main(){
int t, B; //no. of testcase and base of a triangle
int k, n;
cin>>t;
while (t–){
int result;
cin>>B;
k = B/n;
result = (k*(k-1))/2;
cout<<result<<endl;
}
}
please explain clearly b/2-1 came from
how total no of squares is (b-2)/2 I understood till less than b-2 length only square is possible help me …
You should use recursion limit in Python, add following 2 lines top of your code :
import sys sys.setrecursionlimit(10**7)
VVGoodEditorial:p
CodeChef: Practical coding for everyone i am getting runtime error can anyone plz tell me what’s wrong in my code