I'm very glad for you that you got accepted, but in the process of getting there you created four new threads for a problem that has many threads already. Please conform to the forum rules, and only create a new thread if there is no existing thread for your problem, otherwise post in an existing thread.
most problems in UVA will show Time Limit Exceeded if they run for more than 10 seconds, so maybe you are misunderstanding the meaning of your result, it means that your program ran during 10.1 seconds, and as it didn't finish you got Time Limit Exceeded, the exact time your program solved the problem is unknown
It generates all relatively prime triples, not sure where that "some" part came from. Some numbers occur in more than one triple, but they are still relatively prime.
I just solved this, generating relatively prime triples is ok (my source is Ore's Number Theory and Its History), but counting unused numbers takes forever (5s+ with Java).
There seems to be a better way than to just count them every time, but I can't come up with it. I thought about marking them all, but then, for instance, 6 would be marked (because (3 4 5) -> (6 8 10)), but if n==6, it should be counted as unmarked.
Any primitive Pythagorean triplet (m,n,p) is of the form
p=x*x+y*y
m=x*x-y*y
n=2*x*y
where x and y are co prime and at least one of x and y is even. Also all other Pythagorean triplets are simply obtained by taking multiples of these. These are well known results from number theory, and should be used to solve this problem.
def prime(i, j, k):
myMax = max([i, j, k])
for l in range(2, myMax):
if i%l==0 and j%l==0 and k%l==0:
return False
return True
file = open('input.txt')
for line in file:
nTriple = 0
n = int(line.strip())
triple = [0 for i in range(n+1)]
for i in range(1, n-1):
for j in range(i+1, n):
for k in range(j+1, n+1):
if i*i + j*j == k*k:
triple[i], triple[j], triple[k] = 1, 1, 1
if prime(i, j, k):
nTriple += 1
notPartOfTriples = 0
for i in range(1, n+1):
if triple[i] == 0:
notPartOfTriples += 1
print nTriple, notPartOfTriples
file.close()
Its slow but it works (with the problem statement examples that is lol)
int main()
{
unsigned long n;
unsigned long i,j,k;
unsigned long sqrt_n;
unsigned long n_prime;
unsigned long n_elements;
unsigned long sq_i,sq_j,x,y,z;
I tried all the inputs I found on this board, and the output matches the correct output, but I'm still getting WA on this one. Some sample input/output here.
Input: