# Thread: want formula for number of moves to invert 'triangle'.

1. Originally Posted by mido22
here is the correct formula:
(n*( n+1 ) /2 )/3)
And that's different from my formula how????????

Note that you must be doing integer division when dividing by 3, otherwise you would get non-integer values. Plus.. your parentheses don't match up.

2. it wasn't different but i must use integer divisor to diplay integers only as u said thx very much to u for help

3. ## hey query for the same question..

what is wrong with my code??....it is still giving wrong answer

#include <stdio.h>
#include <math.h>
#include <iostream.h>

using namespace std;

int main(){
int n;
unsigned long long int t,sol;
//long long int sol;
//int sol;
scanf("%d",&n);
//cout<<n;
for(int i=0;i<n;i++){
//cin>>t;
scanf("%I64u",&t);
//cout<<"t is "<<t<<endl;
sol=t*(t+1)/6;
//sol=sol/3;
//cout<<"sol "<<sol<<endl;
sol=floor(sol);
printf("%I64u\n",sol);
}
system("pause");
return 0;
}

4. Originally Posted by scorpio90
what is wrong with my code??....it is still giving wrong answer

#include <stdio.h>
#include <math.h>
#include <iostream.h>

using namespace std;

int main(){
int n;
unsigned long long int t,sol;
//long long int sol;
//int sol;
scanf("%d",&n);
//cout<<n;
for(int i=0;i<n;i++){
//cin>>t;
scanf("%I64u",&t);
//cout<<"t is "<<t<<endl;
sol=t*(t+1)/6;
//sol=sol/3;
//cout<<"sol "<<sol<<endl;
sol=floor(sol);
printf("%I64u\n",sol);
}
system("pause");
return 0;
}
All my previous posts on this thread were prior to my joining SPOJ; now that I'm a member there, I'm less inclined to give away solutions; also, your post is more appropriate for the SPOJ forums than here.

That said, there are several oddities in your code,

(1) iostream is for C++, not C. You would #include <iostream>, not <iostream.h>. My compiler (g++) reports this as a fatal error.

(2) system("pause") is Windows/DOS-specific, in that it calls the DOS "pause" command, which will not work on POSIX systems (where you could use the bash command "sleep" to get similar functionality, but from testing I don't think this works with SPOJ anyway). Also it requires <stdlib.h> or <cstdlib>, and my compiler refuses to compile without the header. But this line should simply be omitted. For your own convenience you should try to set your system up so that you never have to write this line for SPOJ problems.

(3) The scanf type specifier %I64u is not a universal standard. From what I can tell, MinGW g++ uses %I64u and POSIX g++ uses %llu, for whatever reason. Based on testing, you should use %llu for SPOJ.

(4) Calling floor() shows that you don't understand how integer division works in C/C++. Effectively you are casting from llu to double back to llu, which can result in a loss of precision. (For example, I tested it out on 12345678901234567890, which came back as 12345678901234567168.)

Note that you can use ideone, which is based on the SPOJ engine, to test how your code will compile on the machines used by SPOJ.

As a final note, you can use [code][/code] tags on this forum so that your code shows up properly formatted; you can also use [php][/php] even though you're not posting PHP code, because then you will get syntax highlighting that works pretty well for C-like syntax in general.

Code:
#include <stdio.h>

int main() {
printf("Using 'code' tags\n");
return 0;
}

[php]
#include <stdio.h>

int main() {
printf("Using 'php' tags\n");
return 0;
}
[/php]

5. ## thanks

the problem was the usage of "I64u"...."llu" worked fine for spoj though it was not working in windows properly

thanks for the help!!

Page 2 of 2 First 12