Reputation: 123
I'm trying to get a feel for C++ by creating a program to efficiently find highly composite numbers, but I'm facing an inconvenience. The console waits and then prints everything at once. I want it to print things one at a time like Java and Python do. How do I do that?
Here's the code:
#include <iostream>
int main(){
int record=0; //Highest number of factors found in a number so far
for(int n=1; n<2147483647; n++){
int factors=1;
for(int PPF=2; PPF<=n; PPF++){ //PPF="Possible Prime Factor"
bool isPrime=true;
for(int i=2; i<PPF; i++){ //
if(PPF%i==0){ //Determining if the PPF is prime
isPrime=false; //
break;
}
}
if(isPrime){
if(n%PPF==0){ //Here is where I calculate the number of factors n has based on its prime factorization.
int PRP=1; //PRP="Prime Raised to Power"
int pow;
for(pow=1; n%(PRP*PPF)==0; pow++){ //Finding the exponent of a specific prime factor
PRP*=PPF;
}
factors*=pow;
}else{
break;
}
}
}
if(factors>record){
record=factors;
std::cout<<n; //Print the highly composite number
printf("\n"); //Gotta make a new line the old-fashioned way cuz this is a low-level language
}
}
}
I'm running this on VS Code with CodeRunner and all the necessary C/C++ extensions.
Upvotes: 0
Views: 278
Reputation: 2077
More precisely, you need std::flush
. std::endl
prints a line break and flushes, but flush does so without the break. You can also print to std::cerr
which is not buffered and prints everything directly.
Upvotes: 2