| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
c++ sum of 2 prime numbers
hey...
im tryin to write a code that when input a number n it outputs p and q where p and q are both prime numbers and fits the equation n = p + q...(q = n - p) in ths code im not allowed to use the mod sign (%).. please help... thanks ![]() |
|
#2
|
||||
|
||||
|
[pred]Sounds like you want us to do your homework. At least try something, and then post the code here, so we can tell you what you're doing wrong.[/pred]
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 280
![]() |
|
#3
|
|||
|
|||
|
this is the code i have so far.. how can i change it so i dnt use the mod % sign??
#include <iostream> using namespace std; int goldback(int n, int p, int q); int main() { int N; int P = 3; int Q = 3; while (N != -1){ cin >> N; goldback(N, P, Q); } return 0; } int goldback(int n, int p, int q) { int pprime = 0; int qprime = 0; for (int i = p; i < n; i++) { for (int j = 2; j < i; j++) if (i%j == 0) pprime++; if (pprime == 0) for (int k = q; k < n; k++) { for (int m = 2; m < k; m++) if (k%m == 0) qprime++; if (qprime == 0) if (n == (i + k)) { cout << "prime numbers " << i << " and " << k << endl; return 0; } qprime = 0; } pprime = 0; } return 1; } this there any other way to do this??? |
|
#4
|
||||
|
||||
|
Easy way? Replace it with:
Code:
if (((i / j) * j) == i) Make sure not to use any optimizers though, they might kick this out... |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > c++ sum of 2 prime numbers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|