Project Euler #223: Almost right-angled triangles I

Sort by

recency

|

24 Discussions

|

  • + 0 comments

    Can you explain me why this is not getting result against the testcases. Sample one Ok. but others not

    def calTriangles(N):
        triangle = [[a,b,c]for a in range(1,N) for b in range(1,N) for c in range(1,N) if a**2+b**2==c**2+1 and a+b+c<=N and a<=b and b<=c]
        return len(triangle);
    
    
    Q = int(input())
    if Q>=1 and Q<=50:
        for i in range(Q):
            N = int(input())
            print(calTriangles(N))
    
  • + 0 comments

    //What is wrong in my code ????

    include

    include

    include

    include

    include

    include

    include

    using namespace std;

    int main() { int i=0,j=0; int a,b,c,q,n; int tcount=0; cin>>q; for (i=0;i cin >>n; for (i=1;i for(j=i;j auto t=pow(i*i+j*j-1,0.5); if (typeid(t).name()==typeid(int).name()) if (t>=j && i+j+t<=n) tcount++;
    } } } } cout<

  • + 0 comments

    What is wrong in my code ?????

    include

    include

    include

    include

    include

    include

    include

    using namespace std;

    int main() { int i=0,j=0; int a,b,c,q,n; int tcount=0; cin>>q; for (i=0;i cin >>n; for (i=1;i for(j=i;j auto t=pow(i*i+j*j-1,0.5); if (typeid(t).name()==typeid(int).name()) if (t>=j && i+j+t<=n) tcount++;
    } } } } cout<

  • + 1 comment

    I'm getting timeout for all cases except case 0 . How can i imporve my algo??

    test=int(input())
    s=[]
    res=[]
    count=0
    for i in range(test):
        s.append(int(input()))
    for i in s:
        for a in range(1,i//2+1):
            for b in range(1,i//2+1):
                for c in range(1,i//2+1):
                    if(a<=b<=c):
                        if (a+b+c<=i):
                            if (a*a+b*b==c*c+1):
                                count+=1
        print(count)
    
  • + 0 comments

    i have a code taht runs for first 10 test cases.

    		Scanner s = new Scanner(System.in);
    		int Q = s.nextInt();
    		List<Integer> triplet = new ArrayList<>();
    		triplet.add(17);
    		for (int i = 0; i < Q; i++) {
    			int n = s.nextInt();
    			int a, b, c = 2;
    			int lastNum = triplet.get(triplet.size() - 1);
    			int waste = 0;
    			int answer = 0;
    			int ansind = 0;
    			long time1=Calendar.getInstance().getTimeInMillis();
    			if (n > lastNum) {
    				for (a = 2; a < n / 3; a++) {
    					b = a;
    					c = (int) Math.ceil(Math.sqrt(a * a + b * b - 1));
    					for (b = Math.max(a, ((lastNum) / 2) - a); b < (n - 1) / 2 && (c < a + b)
    							&& (a + b + c <= n); b++) {
    						waste++;
    						c = (int) Math.ceil(Math.sqrt(a * a + b * b - 1));
    						// System.out.println("\t\t" + a + ", " + b + ", " + c);
    						if (b == c)
    							break;
    						if (a + b + c <= lastNum)
    							continue;
    						if (a * a + b * b == c * c + 1) {
    							triplet.add(a + b + c);
    							// System.out.println("a=" + a + " b=" + b + " c=" + c + " sum=" + (a + b + c));
    						}
    					}
    				}
    				Collections.sort(triplet);
    			}
    
    			if (n >= triplet.get(triplet.size() - 1)) {
    				ansind = triplet.size();
    			} else if (n > 17) {
    				for (int j = 0; j < triplet.size(); j++) {
    					if (triplet.get(j) > n) {
    						ansind=j;
    						break;
    					}
    				}
    
    			}
    			long time2=Calendar.getInstance().getTimeInMillis();
    			answer = ansind + (n - 1) / 2;
    			System.out.println("waste=" + waste);
    			System.out.println(answer);
    			System.out.println((time2-time1)/(1000));
    			// triplet.stream().forEach(x -> System.out.print(x + ", "));
    		}
    

    Can someone suggest how I can optimize it further. (waste in my code refers to the number of loop iterations. its just a informatory parameter i am using.)