Issue converting python code to c++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Issue converting python code to c++

  1. #1
    Join Date
    Feb 2011
    Posts
    2

    Issue converting python code to c++

    I am trying to solve a project euler prob 26 solution. I stumbled across a link that uses python to solve it. Here is the code snippet:
    The IsPrime function is different which does not seem to cause any issues.
    n = 997 #starting prime closest to limit
    for p in range(n, 1 ,-2):
    if not is_prime(p): continue
    c = 1
    while (pow(10, c) - 1) % p != 0:
    c += 1
    if (p-c) == 1: break

    print "Answer to PE26 = ",p

    I want to execute the same program on C++. But, the program does not complete(keeps on looping on the while loop below). I donít know python that well. Can it be explained to me if the following code snippet is right in c++?

    int p = 0;
    int n = 997;
    int c=0;
    for ( p=n;p>1;p-2)
    {
    if(!IsPrime(p))
    continue;

    c=1;
    while (((int)pow(10.0,c) Ė 1) % p != 0)
    {
    c += 1;
    }

    if (p-c==1)
    break;

    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    Re: Issue converting python code to c++

    Please post code in [code][/code] bbcode tags. This is especially important for Python code since indentation can be significant.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center