C++ :: Prime Factors Of A Number?
Oct 22, 2013
I have to create a program where I need to find the prime factors of a number and express it using exponents.
For example, for the number 1368 should output:
1368 = 2^3 * 3^2 * 19^1
This is my code:
void primeFact (int n){
int i;
int score = 0;
[Code].....
View 3 Replies
ADVERTISEMENT
Mar 18, 2015
I am trying to find the prime factors of a number but when i compile and run my code after i enter a number it just closes it doesn't print ....
included library : [URL] ....
#include "std_lib_facilities.h";
void prime(int n) {
int result;
result = 0;
int i;
for (i = 2; i == n*n; ++i)
[Code] .....
View 14 Replies
View Related
Mar 23, 2013
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;
[Code] .....
View 1 Replies
View Related
Apr 21, 2014
find the prime factors of the number input by the user. This time, we are going to have the user input a number, and the program will find all prime numbers from 2 to that number and prime factors of the number input by the user
#include <iostream>
#include <cmath>
using namespace std;
[Code]......
View 5 Replies
View Related
Jan 6, 2015
I am working on an assignment to enter a number and print all the prime factors of that number. I have that working, but the assignment demands the output be formatted in a strange way which I can't figure out. For example, in my current program entering 10 gets me 25, which is actually 2 and 5. But it should get me: ( 2 * 5 ) but I can't figure out how to do this. On the chance you need it, my code is below:
Code:
#include <iostream>
using namespace std;
int main( )
{
cout << "Number: ";
[Code].....
View 12 Replies
View Related
Mar 6, 2015
main function:
Code:
int main(){
int n, s = 0;
printf("Insert number: "); scanf("%d", &n);
if (n == 0 || n==-1 || n==1){ printf("No prime factors!
"); return 0; }
if (n < -1) { n = - n; printf("Prime factors: -"); }
[Code] ....
Recursive function
Code:
static int i = 2;
int primefactors (int n) {
if (n == 1) return 0;
if (n%i == 0) {
printf("%d ", i);
return i + primefactors(n / i);
[Code] ....
View 8 Replies
View Related
Sep 16, 2014
As an assignment for school , I've to write a program to find the sum prime factors of a user input integer.
E.g. 20 = 2 x 2 x 5 , Sum = 2 + 2 + 5 = 9
E.g. 10 = 2 x 5 , Sum = 2 + 5 = 7
My method for finding the result is as follows :
- Divide the number by increasing values if int i , starting from i=2.
- Once I get a value of i that can divide the number without giving me a remainder , I add this value of i to int sum and divide the number by i.
- I will repeat this process until the user input value is equal to 1.
My code is as shown:
#include<stdio.h>
int primecheck(int n); // Function to check if i is prime
int primesum(int n); // Function to sum the values of i that are prime
int main(void) {
int n;
int sum;
printf("Enter a number (> 1): "); //Prompting and scanning user input ,n
scanf("%d",&n);
sum = primesum(n);
[Code] .....
But for some reason I keep getting an incorrect result, it's as if it is missing out the last factor for each case.
Eg. 20 = 2 x 2 x 5 , the result I get is 4 , which is 2+2
Eg. 40 = 2 x 2 x 2 x 5 , the result I get is 6 , which is 2+2+2;
I've looked through my code numerous times.
View 7 Replies
View Related
Jul 13, 2013
What is the most efficient algorithm for finding how many factors a number has? I've just been doing brute force division up to (n - 1) / 2 thus far. How can this be optimized?
View 5 Replies
View Related
Dec 2, 2014
I have a program that gets prime numbers that doesn't work. It looks like it works for all prime numbers except odd non-prime ones. Here is the code:
Code:
#include <iostream>
using namespace std;
bool isPrime(int prime) {
while(true) {
int track = 0;
[Code] ...
View 3 Replies
View Related
Aug 24, 2014
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.
View 1 Replies
View Related
Nov 3, 2013
I need to write a program that will allow the user to enter a number "n" and the program tell you that the nth prime number is .....
EXAMPLE
user enters 55
printf("The 55th prime number is %i", variable");
View 1 Replies
View Related
Nov 20, 2014
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:
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int num);//function prototype
[Code] ....
View 2 Replies
View Related
Nov 7, 2013
#include <iostream>
using namespace std;
int main() {
//input output
[code]....
its supposed to tell whether the inputted number is prime or not
View 1 Replies
View Related
Dec 9, 2013
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;
[code].....
View 1 Replies
View Related
Mar 14, 2014
I wrote this program to print prime numbers between 2 and 10 but it only prints '3'. here is the code
#include<iostream>
#include<conio.h>
using namespace std;
int prime(int x, int y);
void main(){
int n=2;
[code].....
View 6 Replies
View Related
Mar 14, 2015
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.
View 4 Replies
View Related
Oct 3, 2013
cin.ignore (10);
return 0;
}
Its not working
View 2 Replies
View Related
Feb 23, 2014
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
View 8 Replies
View Related
Oct 3, 2013
We need it to say if the number is prime or not we need the equation for the if statement or if we have something else wrong to let me know.
#include <iostream>
using namespace std;
int main () {
int number;
bool countr;
for (countr = 0; countr <= 2; countr ++)
[Code] ....
View 1 Replies
View Related
Mar 13, 2014
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;
View 4 Replies
View Related
Oct 19, 2014
Can I possibly get a better implementation of a prime number detecting algorithm than the one I already have below?
Code:
#include <cstdio>
#include <cstdlib>
int main() {
int no, high, low;
int divisor;
[Code] .....
View 8 Replies
View Related
Feb 17, 2013
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?
View 7 Replies
View Related
Oct 10, 2013
how would I write a while() loop that calculates the sum of all prime numbers from 1 - 50.
View 5 Replies
View Related
Oct 20, 2014
#include "stdafx.h"
#include "iostream"
using namespace std;
int main (){
int x;
int i;
int e;
[Code] ....
is the logic good ? and why the program is looping
View 3 Replies
View Related
Dec 24, 2014
This shows any prime number between a and b and also counts them*/
#include <iostream>
using namespace std;
int main(){
int a;
int b;
[Code] .....
View 1 Replies
View Related
Jan 12, 2013
What is the minimum range of numbers which we need to go necessarily to check either a number is prime or not?(for all integers) ....
View 6 Replies
View Related