You are here:

# C++/c++

Question
A wonder prime is a number that can be partitioned into two prime numbers,each of which has at least D digits and,of course,doesnt start with 0.When D=2,the number 11329 is a wonder prime (since it connects 113 and 29,both of which are prime).I must find the first wonder prime greater than or equal to a supplied integer N when you are given D.
sample input:2 11328
sample output:11329

my version of program is:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int x,y,N,m,n,D;
int sum1=0;int sum2=0;
cin>>N;

for (int x=pow(10,D-1);x>=pow(10,D);x++)
{
for (int m=2;m=sqrt(x);m++)
{
if (x%m==0)
sum1+=m;
}
}
for (int y=pow(10,D-1);y>=pow(10,D);y++)
{
for (int n=2;n<=sqrt(y);n++)
{
if (y%n==0)
sum2+=n;
}
}

if (sum1=sum2=0)
cout<<x<<y<<endl;

system("PAUSE");  return 0;
}

errors:
10 C:\Documents and Settings\Admin\Рабочий стол\C++ samples\not working\wonder primes.cpp call of overloaded `pow(int, int)' is ambiguous
12 C:\Documents and Settings\Admin\Рабочий стол\C++ samples\not working\wonder primes.cpp [Warning] converting to `int' from `double'
18 C:\Documents and Settings\Admin\Рабочий стол\C++ samples\not working\wonder primes.cpp call of overloaded `pow(int, int)' is ambiguous

what is wrong here?
will be grateful for correct program

Hello

1) -------------------------------------------------------------------
In C++, it is possible to have many functions with the same name, but taking different types. For example, there are many different pow functions. You can see all of them at
http://www.cplusplus.com/reference/clibrary/cmath/pow/

Some take double floating point values, some take float values. You are calling pow(10, D) and both types are integers. There is no pow function which takes two integers, and the compiler does not know if you mean to convert the 10 to double, or float. That is why the call is ambiguous. To fix it, cast the base  to something which pow can accept. For example:

pow( (float)10, D)

The (float) changes the integer 10 to a floating point value.

You could also write it like this:
pow( 10.0, D)
By adding the decimal point, the number automatically becomes a double floating point value.

2) -------------------------------------------------------------------
Be very careful when comparing things in C and C++. a=b is not a comparison, it is an assignment.
To compare, use a==b.
You see in for (int m=2;m=sqrt(x);m++), that m=sqrt(x) is an assignment, not a comparison.

if (sum1=sum2=0) is not a comparison. The comparison you are looking for is if (sum1 == 0 && sum2 == 0)

3) -------------------------------------------------------------------
You should practice creating functions to do common tasks. For example, the code
for (int n=2;n<=sqrt(y);n++)
{
if (y%n==0)
sum2+=n;
}
is trying to determine if a number is prime. The code is repeated twice, and the first time it has the error I described in part 2. Repeated code means more confusing code and more chances for errors. You should create a function which describes what the algorithm is trying to do.
For example

// function isPrime, returns true if a number is prime, false otherwise
bool isPrime(int n)
{
// here you put your code to see if a number is prime.
// your method: for i = 2 to sqrt(n) can be made faster
// do you really need to increment by 1?
// once you determine that a number is not prime, do you really need to go all the way to sqrt(n)
}

Now your main program can be written more clearly, like this

int main()
{
int x,y,N,m,n,D;
int sum1=0;int sum2=0;
cin>>N;

bool isPrimeX = false;
bool isPrimeY = false;

for (int x=pow(10,D-1);x>=pow(10,D);x++)
{
isPrimeX = isPrime(x);
}
for (int y=pow(10,D-1);y>=pow(10,D);y++)
{
isPrimeY = isPrime(y);
}

if (isPrimeX && isPrimeY)
cout<<x<<y<<endl;

system("PAUSE");  return 0;
}

You see how you are replacing a complicated loop with human language.

4) -------------------------------------------------------------------
My points 1,2, and 3 show you how to write a program in a better way. However, the logic of your main program is not correct.

sample input:2 11328
11328 is a wonder prime if 11 and 328 are primes, or if 113, and 28 are primes. Those are the only numbers you need to check.

The program you sent me does not do that.

You need a loop to generate the different partitions. Start by creating a program which will have a loop to create the partitions. You need only 1 loop. Use integer division, and the modulo operator (%) to split the original number into two parts. Here is a start for you:

#include<iostream>
#include<cmath>
using namespace std;

/* DigitsLen
returns the number of digits in a number N
when that number is represents in base 10
*/
int DigitLen(int N)
{
// This is an exercise for you
}

int main()
{
int N = 113284;
int D = 2;
int digitsInN = DigitLen(N);

cout << "Original number " << N << endl;
cout << "Digits in number " << digitsInN << endl;
cout << "Min partition length is " << D << endl;

int minPartition = (int)pow(10.0,D);
int maxPartition = (int)pow(10.0, digitsInN - D);

for(int i = minPartition; i <= maxPartition; i = i * 10)
{
int part1 = N / i;
int part2 = N % i;
cout << "Partition: " << part1 << " , " << part2 << endl;
}
}

When you can generate all the partitions of a number, you can determine if the number is a wonder prime.
The above main program is only for you to learn about the problem. It is not the final main program.

Once you have the above program working, take what you have learned, and put it into a function like this:
bool isWonderPrime(int N, int D)
{
// partition N with D, and for each partition pair, use isPrime to see if they are primes.
// return true or false
}

Then your main program becomes a simple loop to generate candidates, like this:

int main()
{
int N = 11238;
int D = 2;

do
{
N = N + 1;
} while ( ! isWonderPrime(N,D));
cout << "Found a wonder prime. It is " << N << endl;
}

Best regards
Zlatko

C++

Volunteer

#### Zlatko

##### Expertise

No longer taking questions.

##### Experience

No longer taking questions.

Education/Credentials
No longer taking questions.