C/C++ :: Prime Factorization Program?
Mar 12, 2014
the only thing is I want multiple all prime number I got. such as when I enter 12 the output should be "the prime factorization of 12 is 2*2*3" right now the program output is "2 2 3"
# include <stdio.h>
# include <math.h>
void primeFactors(int n)
{
[Code]....
View 8 Replies
ADVERTISEMENT
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
Feb 27, 2014
So, I have been working my way through lines of code, and practicing quite a bit. In fact, I have done programs far more complicated than this and still I cannot get it to work.
The idea of the code is to take a number input by the user through the terminal and check if it is a prime number(It does this) If the number is not prime it takes that number and checks for N amount of primes that is put in through the console. When a prime is found it writes that to the console until the N amount of primes have been found.
#include <iostream>
using namespace std;
int prime(unsigned long long x);
int main() {
long long x;
long long y;
long long z;
[Code] ....
At least that's my goal. Currently it will accept the number and check if it is prime. It will then ask for the N amount of primes but will not return any primes. I have check with numbers a I know have prime factors. I have added the comment //XXXXXXXX To show where the problem appears to occur.
View 1 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
Feb 8, 2014
I wrote a function to do factorization (n!). while this function works perfectly:
Code:
int fact (double x){
register int z=1;
if (x==0)
return 1;
else
for(x;x;x--)
z*=x;
return z;
}
this doesn't:
Code:
int fact (double x){
if (x==0)
return 1;
else
for(x;x;x--)
x*=x;
return x;
}
what makes the second one different from the first one? can't see the difference..
View 1 Replies
View Related
Oct 16, 2013
Can not seem to get the program to perform the calculations within the cases. Also, how do you set up the a prime function program within the menu. Have been trying for five days now.
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
void showChoices();
double prime(double, double);
double abs(double, double);
double pow(double, double);
[Code]...
View 1 Replies
View Related
Oct 4, 2014
This is a program that display prime numbers from 1 to 100. it has problem linking when i run the program.
#include <iostream>
class Prime {
public:
int PrimeNo();
int Display();
};
// class Prime number
int Prime::PrimeNo()
[Code] ....
View 3 Replies
View Related
Jan 29, 2013
Here is what I have:
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
using std::cin;
[Code] .....
This is the output after compiling when I enter 10 as 'x':
3
5
5
5
7
7
7
7
7
9
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
May 29, 2013
I'm trying to write a program that prints all of the prime numbers to the screen. I'm getting angry because it only prints EVERY number. I think it has something to do with the bool change not being able to leave the for-loop but how to fix it.
Code:
#import <stdio.h>
#include <stdbool.h>
int main(void){
printf("
The Primes Are: 1");
[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
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
Nov 11, 2013
how to make a programm in native c++ which print out all prime numbers less than or equal to given number????????
View 5 Replies
View Related
Mar 2, 2013
In 83rd line message says: "Illegal Structure operation". But I didn't use any structures! How to correct it?
#include<iostream.h>
#include<conio.h>
#include<math.h>
int prime(int);
int reverse(int) ;
int power(int,int);
int rectangle(int,int);
int square(int);
int circle(int);
void tables(int);
[Code] ....
View 6 Replies
View Related
May 2, 2014
So I need to write a program which tells the user if the number they input is prime or not. I have done so and included the code below. My problem is that I need to allow the user to input if they want to enter another number after the first one using y or n and I am having trouble figure it out.
#include <cstdlib>
#include <iostream>
using namespace std;
[Code].....
View 5 Replies
View Related
May 21, 2012
I have been writing a program to generate pairs of RSA keys using small prime numbers. I have been using mpz_invert() to get a value for d for the private key. On small prime numbers it calculates the correct value, but on larger primes it calculates incorrect values.
I'm assuming there must be an upper limit to the values the mpz_invert() function can handle? If so, what is this limit to avoid erroneous values?
View 1 Replies
View Related
Apr 28, 2013
I wrote a program which sends a starting and ending range to other processes and the processes calculate the prime numbers in that range and return the count of prime numbers to the head process, process 0. But this is not working properly at the moment. I realize I still have to split up the range based on how many processes I have...I still have not figured out how I want to set that up. I
Code:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int isPrime(int num);
int main(int argc, char **argv){
}
[code]....
View 7 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
Feb 21, 2014
Code:
#include<stdio.h>
int main(){
int num,i,count,min,max,sum=0;
printf("Enter min range: ");
scanf("%d",&min);
[code]....
when I ran the program , I got "4" with prime numbers. I only way I was able to remove the "4" was this "num!=4".
View 1 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
May 27, 2013
I was going through a book I have about C trying to learn about arrays, and one of the first few examples of what could been done with an array was showing how to use an array to generate a list of prime numbers. I've been staring at this program for about an hour trying to understand how it works (it does), but the workings of one for loop within the program.
Code:
#include <stdio.h>
#include <stdbool.h>
int main () {
int p, i, primes[50], primeIndex = 2;
bool isPrime;
[Code]...
So that is saying in order for this loop to go on, really two conditions must be met since there's that && operator. isPrime must be true (I think that's what it means by just having "isPrime" and not setting it equal to anything) and p / primes[i] must be greater than or equal to primes[i].So at the beginning of the loop, since i = 1, p = 5 (as per surrounding loop), and prime[i] = 3 ( as per the variable assignment at the beginning of the program ), the loop condition would be "isPrime && 5 / 3 >= 3"
"5 / 3 >= 3" The loop should stop right there! 1.666666667 is NOT greater than or equal to 3!
View 14 Replies
View Related
Feb 3, 2014
Code:
#include <stdio.h>
int find_next_prime(int num);
int is_prime(int num);
int main()
[Code]......
How would I go about counting the number of times a factorial has a specific prime number?
For example, 5! = (2^3)*(3^1)*(5^1), 6! = (2^4)*(3^2)*(5^1).
How would I begin to design my find_prime_count function in order to count how many times each occurs? My program is to read in a number between (2<=N<=100) from a text file and output the results exactly like above which I still have to figure out after, I'll assume I have to use fscanf.
View 8 Replies
View Related
Oct 15, 2013
i'm trying to determine if a number from a file is a prime number using pointers first I wrote a function that determines if the number is a multiple of 7, 11, or 13. Then i wrote a function to see if the number is odd or even. Are they correct? Later i will print the results on screen but i'm extremely confused with pointers and i'm not sure how to write this prime function...
Code:
void divisible(int *n, int *result) {
if (*n % 7 == 0 || n % 13 == 0 || n % 13 == 0) {
*result = 1;
[Code] .....
View 2 Replies
View Related
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
View Related
Nov 4, 2013
I am trying to print out prime from array. I have viewed other sources that are similar and tried to use as reference, but only to confuse myself. This is what i have so far.
void get_output(int&Max,int array[]) {
//array[i]-i;
for(i=1;i<=10;i++) // allow 10 outputs on a line. {
for(int i=0;i<Max;i++) {
prime=true;
[Code] ......
View 4 Replies
View Related
Feb 17, 2013
I have an assignment where I have to use two for loops. I need to ask the user for any two numbers and be able to list all the numbers in between and their factors and state whether or not the number is prime or not.
View 2 Replies
View Related