use the code tags, eases reading
your first for loop does nothing
for(int k=1;m=n;k++){
your code needs more changes, but Im going to assume you want to find the primes between M and N, two input numbers.
your first loop would look like
for(int k=m; k<=n; k++){
this sets the start of the loop at the first integer (m) and runs until it reaches the second integer (n) increasing by one each time.
then within this loop you have another loop
for(i = 2, i < k; i++)
, iterate between every number from 2 to k-1 and checking if the expression m%i == 0, if it comes up true, break out of the loop and start over the first loop again, if it doesnt come up true and the inner loop ends, add the current k to an array or a string, and then continue checking through the numbers
once the loops end you can then output the string or char array holding your prime numbers.
I like the thought of just checking for numbers between 2 and n-1.. I made a similar program but I checked from 1 to the number itself, but I did expand it to check for any number of factors.. it does only check from 1-1000, but adding in the functionality to check for numbers with a given number of factors within a given range would be easy.. heres the code to mull over if you like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
//Finds numbers between 1 and 1000 with a given number of factors
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
inline string toStr(int a){
return static_cast<ostringstream*>(&(ostringstream()<<a))->str();
}
int main() {
while(true){
string outStr;
int factors;
int totalNums = 0;
cout<<"Find numbers between 1 and 1000 with how many factors?"<<endl;
cin>>factors;
cout<<endl;
//check for invalid entry
if(!cin){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"That's not a number! :(\n\n";
continue;
}
outStr = "Numbers with a total of " + toStr(factors) + " factors:\n\n";
//begin iterating through numbers
for(int i = 1; i < 1000; i++){
int numEvenDivs = 0;
//iterate through possible factors
for(int j = 1; j <= i; j++){
if(i%j == 0){
//if there are more factors than we're looking for, break out of loop
if(++numEvenDivs > factors)
break;
}
}
//display number and its factors
if(numEvenDivs == factors){
++totalNums;
outStr += "> " + toStr(i) + ": ";
for(int k = 1; k <= i; k++){
if(i%k == 0){
outStr += toStr(k)+ " ";
}
}
outStr += "\n";
}
}
//if there arent any numbers at all
if(totalNums == 0){
cout<<"No such numbers exist!\n\n";
continue;
}
//final output to console
cout<<outStr<<endl;
cout<<"A total of "<<totalNums<<" numbers exist with "<<factors<<" factors\n\n";
//check to see if user wants to output to a text file
cout<<"Would you like to output the results to a text file, type Y or N"<<endl;
char getResults;
cin>>getResults;
if(getResults == 'Y' || getResults == 'y'){
ofstream out(string(toStr(factors) + "-factors.txt").c_str());
out<<outStr;
cout<<"Results output!\n\n";
} else {
cout<<"\n";
}
}
}
|
note im just a beginner too so im sure theres some not so "proper" stuff in there.. not sure shrug! youll notice i do a second loop to output the factors after I know a number has the given number of factors im looking for, this seems at first, but otherwise I would have to keep note of each factor as I went along, whether the current number ended up with more or less than the number of factors I wanted.. I guess I could have just had a 'buffer' stirng to hold this information and then if it checked out append it to the output string and then clear the buffer.. but this seemed like it might be less work overhead..
goodluck