I'm a new coder for C++, and I've recently learned java before starting this programming language. I'm attempting to find all prime numbers up to the number a user enters (i.e. User enters 10, System shows "1,2,3,5,7"),
#include <iostream> #include <cstdlib> using namespace std; int main(int argc, char** argv) { int num; cout << "Enter a positive number." << endl;
[Code] ....
I've been looking at my forloop, and I can't seem to pinpoint the problem with it.
I am attempting to write code that receives a number from the user and outputs the largest prime number underneath the user's number. There are no errors in my code, but no matter what number is imputed, the program says the largest prime number is 1. I cannot find where the issue is in the code. Here is the code I wrote:
The code below is suppose to output all the prime numbers between the values of startNum and endNum variables. but its not working correctly instead it display all the numbers between startnum and endNumber including non-prime numbers.
I am trying to factorize semi-prime numbers by using a brute force method and a square root method.The brute force method should start by multiplying all odd numbers between 1 and the semi-prime number. Meaning that 1 should multiply 3 then 5 then 7, etc. Then three should multiply 5 then 7 then 9 and so forth. The first number would never multiply a number equal to or less than itself.
bruteForce(int x){ f1 = 1; f2 = f1 + 2; while (f1 < x){ for (f2 = x; f2 <= x; f2-2){
[Code] ....
For my other algorithm, I want it to get the square root of the semi-prime and then start multiplying odd numbers around it. I made it so that the square root would have to be set to the next even number (in case it was a factor). If the product of the two odd numbers (a * b) is greater than the original semi-prime, I would multiply b by the next smaller number than a. Conversely, if the product of a and b is less than the original semi-prime, it will multiply a by the next odd number larger than b. This will repeat until the two correct factors of the original semi-prime are found.
Algorithm:
SquareRootFactorize(int x){ int N = x; sqrtN = sqrt(N); if (isOdd(sqrtN) == true) sqrtN = sqrtN + 1; f1 = sqrtN - 1; f2 = sqrtN + 1;
We are working on building a cluster and needed to create or find a program writen in C that we can run. I was thinking a program that finds prime numbers and insert them into a MySQL database. I have never worked with C so I don't know where to start or anything. I have a week to prove our project is worth keeping and our cluster is almost completely installed we just need to create a program.
I have to write a program that asks the user to input a positive integer, counts how many primes there are before this integer, then divide the number of primes by the integer. Here's what I have so far:
int n; int counter = 0; cout<<"Enter a positive integer n."; cin>>n;
[Code]....
the value of the integer counter is supposed to increase whenever a prime number is identified but it's increasing n times
my assignment is to print out the numbers from 2 to 20 stating if they are primes or not using loops and/or a function..once i execute, it throws the program into an infinite loop...
#include <iostream> #include <cmath> using namespace std; bool prime(int n); int main() { int i;
Basically, I am suppose to compute 10 numbers and find their highest prime number for each of them. I've already have the inputs working, and set up 2 arrays to store inputs and their highest prime number. But I am uncertain on how to find and store their highest prime number. How to start it off?
i want to write a program which find the biggest prime factor of a number for example the biggest prime factor of six is three or the biggest prime factor of fifteen is five. What is my program bug
Code:
#include <stdio.h> // main functions #include <math.h> // for sqrt function int main() { int i, j, k, f; // F = Flag; printf("Enter K
Write a program that prompts the user to enter a number larger than 2. The program should use the Number class to determine the prime numbers between 2 and the number entered, and display them in the console window.I've been stuck for a while now and just lost in implementing classes and contstructors.
#include <iostream> using namespace std; int main(int argc, char * argv[]) { cout << "Enter a number larger than 2: " << endl; int n; cin >> n;
What is the largest prime factor of the number 600851475143 ?
This is what I have. If I replace 600851475143 by 1000 (for example), it works fine until 2 divides 1000 to 500, 250, 125...but when it has to find the next divisible prime (ie 5) it cannot do that and the program stops working.
Code:
#include<iostream> using namespace std; bool find_prime(int number) { int factor, num; factor = 2; num = number;