Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Math Help - want formula for number of moves to invert 'triangle'.

  1. #16
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Quote Originally Posted by mido22 View Post
    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.
    Follow Math Help Forum on Facebook and Google+

  2. #17
    Member
    Joined
    Jun 2010
    From
    Zewail University - Cairo - Egypt
    Posts
    139
    it wasn't different but i must use integer divisor to diplay integers only as u said thx very much to u for help
    Follow Math Help Forum on Facebook and Google+

  3. #18
    Newbie
    Joined
    Dec 2010
    Posts
    2

    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;
    }
    Follow Math Help Forum on Facebook and Google+

  4. #19
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Quote Originally Posted by scorpio90 View Post
    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]
    Follow Math Help Forum on Facebook and Google+

  5. #20
    Newbie
    Joined
    Dec 2010
    Posts
    2

    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!!
    Follow Math Help Forum on Facebook and Google+

Page 2 of 2 FirstFirst 12

Similar Math Help Forum Discussions

  1. Replies: 3
    Last Post: June 23rd 2011, 02:30 AM
  2. Invert this ellipsoid formula
    Posted in the Geometry Forum
    Replies: 1
    Last Post: March 9th 2011, 01:14 PM
  3. How to invert binary number in schematic?
    Posted in the Discrete Math Forum
    Replies: 0
    Last Post: November 3rd 2009, 06:09 PM
  4. Replies: 3
    Last Post: October 7th 2009, 10:24 AM
  5. Aparticle moves.......
    Posted in the Calculus Forum
    Replies: 2
    Last Post: January 3rd 2008, 03:31 PM

/mathhelpforum @mathhelpforum