Simple simple program error...please help

Discussion in 'C++' started by tasheeta@gmail.com, Nov 1, 2005.

  1. Guest

    In my simple program I am getting this error..please help

    I am trying to find integers where 65537i + 3551j = 1

    error: cannot convert `__complex__ int' to `long int' in
    assignment


    #include <iostream>
    #include <complex>
    #include <cmath>

    using namespace std;

    int main ()
    {
    long x=0;
    int y=0;

    for (long i=0; i<65537; i++)
    {
    for (long j=0; j<3511; j++)
    {
    x=65337i + 3511j;
    if (x=1)
    cout <<"i: "<< i << " j: "<< j<< endl;
    }
    }

    return 0;
    }
    , Nov 1, 2005
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    > In my simple program I am getting this error..please help
    >
    > I am trying to find integers where 65537i + 3551j = 1
    >
    > error: cannot convert `__complex__ int' to `long int' in
    > assignment
    >
    >
    > #include <iostream>
    > #include <complex>
    > #include <cmath>
    >
    > using namespace std;
    >
    > int main ()
    > {
    > long x=0;
    > int y=0;
    >
    > for (long i=0; i<65537; i++)
    > {
    > for (long j=0; j<3511; j++)
    > {
    > x=65337i + 3511j;


    What is this? 3511j is not a number. 65537i is probably not a number,
    although it might be 65537 as an int.

    I think you want x = i + j; here

    > if (x=1)
    > cout <<"i: "<< i << " j: "<< j<< endl;
    > }
    > }
    >
    > return 0;
    > }
    >
    Jim Langston, Nov 1, 2005
    #2
    1. Advertising

  3. Kai-Uwe Bux Guest

    wrote:

    > In my simple program I am getting this error..please help
    >
    > I am trying to find integers where 65537i + 3551j = 1
    >
    > error: cannot convert `__complex__ int' to `long int' in
    > assignment
    >
    >
    > #include <iostream>
    > #include <complex>
    > #include <cmath>


    ditch the last two headers: you do not use them anyway.

    >
    > using namespace std;
    >
    > int main ()
    > {
    > long x=0;
    > int y=0;
    >
    > for (long i=0; i<65537; i++)
    > {
    > for (long j=0; j<3511; j++)
    > {
    > x=65337i + 3511j;


    make that:

    x= 65337*i + 3511*j;

    > if (x=1)


    probably you mean:

    if ( x == 1 )

    > cout <<"i: "<< i << " j: "<< j<< endl;
    > }
    > }
    >
    > return 0;
    > }


    Also:

    a) running the corrected program, you might be in for a little surprise. It
    will not find any numbers doing the trick: you are arbitrarily restricted
    the search space for i and j, and your bounds are way off (note that it
    simply cannot work for i and j both positive!).

    b) You might consider reading on GCDs and the Euclidean Algorithm. It can be
    extended to solve your problem.



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 1, 2005
    #3
  4. toy Guest

    Kai...thanks so much for your advice

    why am i restricted? i thought that c++ integers can span a certain
    space..along the lines of 2^32..???

    i'm familiar with the algorithms, am i required to write my own class
    to make this work?
    toy, Nov 1, 2005
    #4
  5. Kai-Uwe Bux Guest

    toy wrote:

    > Kai...thanks so much for your advice


    It's Kai-Uwe.


    > why am i restricted? i thought that c++ integers can span a certain
    > space..along the lines of 2^32..???


    Please quote what you are refering to. I wrote:

    > a) running the corrected program, you might be in for a little surprise.
    > It will not find any numbers doing the trick: you are arbitrarily
    > restricted the search space for i and j, and your bounds are way off
    > (note that it simply cannot work for i and j both positive!).


    And this in not even correct English. I should have written:

    .... you arbitrarily restricted the search space for i and j, ...


    > i'm familiar with the algorithms, am i required to write my own class
    > to make this work?


    *Which* algorithm? To make *what* work?


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 1, 2005
    #5
  6. toy Guest

    Kai-Uwe, I apologize for abbreviating your name. If I change the search
    space for i and j to count downward, (as in i--, j--)..will this work?

    or need i write a class or input code to execute the euclidean
    algorithm in some form?
    toy, Nov 1, 2005
    #6
  7. toy Guest

    Ok I changed the code to decrement i and j as opposed to
    increment...the for loops will also execute until i and j are their
    NEGATIVE values.

    i still am not generating a solution. can u help?
    toy, Nov 1, 2005
    #7
  8. Kai-Uwe Bux Guest

    toy wrote:

    > Ok I changed the code to decrement i and j as opposed to
    > increment...the for loops will also execute until i and j are their
    > NEGATIVE values.
    >
    > i still am not generating a solution. can u help?


    Sure. Here is a simple version of Euclids algorithm:

    #include <iostream>
    #include <algorithm>

    unsigned long gcd_euclid ( unsigned long a, unsigned long b ) {
    if ( b < a ) {
    std::swap( a, b );
    }
    while ( a != 0 ) {
    // now b = a*q + r for some q and r. (division with remainder)
    unsigned long q = b / a;
    unsigned long r = b % a;
    std::cout << r << " = " << b << " - " << a << "*" << q << '\n';
    b = a;
    a = r;
    }
    return ( b );
    }

    int main ( void ) {
    std::cout <<gcd_euclid( 65537, 3551 ) << '\n';
    }

    If you run this, you find: the output

    1619 = 65537 - 3551*18
    313 = 3551 - 1619*2
    54 = 1619 - 313*5
    43 = 313 - 54*5
    11 = 54 - 43*1
    10 = 43 - 11*3
    1 = 11 - 10*1 <--- important information
    0 = 10 - 1*10


    The magic of the algorithm is that all these equations are actually true.


    Now, you can work backwards:

    1 = 11 - 10 * 1;
    = 11 - ( 43 - 11 * 3 ) * 1 = 11 * 4 - 43
    = ( 54 - 43 ) * 4 - 43 = 54*4 - 43*5
    = 54*4 - ( 313 - 54*5) * 5 = 313*(-5) + 54*29
    = ...

    Another way is working forward:

    1619 = 65537 - 3551*18
    313 = 3551 - 1619*2 = 3551 - ( 65537-3551*18 ) * 2
    = 65537*(-2) + 3551*37
    54 = 1619 - 313*5
    = ( 65537-3551*18 ) - ( 65537*(-2) + 3551*37 ) * 5
    = 65537*someting + 3551*something_else
    ....
    keep going until you get
    ....
    1 = 65537*something + 3551*something_else


    These ideas should get you started.

    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 1, 2005
    #8
  9. toy Guest

    you are a LIFESAVER

    thanks sooo much!
    toy, Nov 1, 2005
    #9
  10. * Kai-Uwe Bux:
    >
    > [relevant info, gcd]
    >
    > These ideas should get you started.


    Just tell me how you saw what he was trying to do?

    Even if it's off-topic.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 1, 2005
    #10
  11. toy Guest

    @alf what do you mean?

    I understood because I have prior knowledge and familiarity with
    Euclid's algorithm..and his explanation was very clear and concise.

    why do you ask?
    toy, Nov 1, 2005
    #11
  12. Neo Guest

    "toy" <> wrote in
    news::

    >
    >
    >
    >


    Euclid's Algo?? Which one.?
    Neo

    --
    Neo, Nov 1, 2005
    #12
  13. Kai-Uwe Bux Guest

    Alf P. Steinbach wrote:

    > * Kai-Uwe Bux:
    >>
    >> [relevant info, gcd]
    >>
    >> These ideas should get you started.

    >
    > Just tell me how you saw what he was trying to do?
    >
    > Even if it's off-topic.
    >


    Easy: the following line from the original post gives it away:

    I am trying to find integers where 65537i + 3551j = 1

    What follows is code that clearly tries a brute force attack to find i and
    j. After fixing, the relevant portion looks like so:

    for (long i=0; i<65537; i++) {
    for (long j=0; j<3511; j++) {
    {
    x=65337 * i + 3511 * j;
    if (x==1) {
    cout <<"i: "<< i << " j: "<< j<< endl;
    }

    }}



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 1, 2005
    #13
  14. Guest

    Kai-Uwe Bux wrote:
    > Alf P. Steinbach wrote:
    >
    > > * Kai-Uwe Bux:
    > >>
    > >> [relevant info, gcd]
    > >>
    > >> These ideas should get you started.

    > >
    > > Just tell me how you saw what he was trying to do?
    > >
    > > Even if it's off-topic.
    > >

    >
    > Easy: the following line from the original post gives it away:
    >
    > I am trying to find integers where 65537i + 3551j = 1
    >
    > What follows is code that clearly tries a brute force attack to find i and
    > j. After fixing, the relevant portion looks like so:
    >
    > for (long i=0; i<65537; i++) {
    > for (long j=0; j<3511; j++) {
    > {
    > x=65337 * i + 3511 * j;
    > if (x==1) {
    > cout <<"i: "<< i << " j: "<< j<< endl;
    > }
    >
    > }}
    >


    Don't you think he should include some negative integers as well, or he
    might waste computer resources for nothing.

    hint: neither (0,1) nor (1,0) doesn't work, so...
    , Nov 1, 2005
    #14
  15. toy wrote:
    >
    > @alf what do you mean?
    >
    > I understood because I have prior knowledge and familiarity with
    > Euclid's algorithm..and his explanation was very clear and concise.
    >
    > why do you ask?


    I guess because this looks like a homework assignment in basic
    loop handling. And for that, a teacher is not happy with a solution
    based on euclids algorithm (probably because a newbie is not able
    to write one on his own).

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Nov 2, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phrank
    Replies:
    7
    Views:
    414
  2. Replies:
    4
    Views:
    509
    Chris Uppal
    May 5, 2005
  3. KK
    Replies:
    2
    Views:
    557
    Big Brian
    Oct 14, 2003
  4. MuZZy
    Replies:
    7
    Views:
    1,751
    Mike Hewson
    Jan 7, 2005
  5. imaginary
    Replies:
    0
    Views:
    836
    imaginary
    Dec 2, 2009
Loading...

Share This Page