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;
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: ";
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;
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;
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?
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 have written a program that finds the divisers of non-prime numbers and outputs them. However, the output is only one diviser per line(because of the repetition to find the divbisers), but the instructor wants us to have 5 to 10 divisers per line. How can i write this loop so it will only output the divisers in one line?
A prime number is a number greater than 1 which is only evenly divisible by 1 and itself. For this assignment you will find which numbers from 2 to n (where n is a user-specified number) are prime. Ask the user for a number, n, greater than 2. Keep asking for a number until a number greater than 2 is provided. Assume that the user will only enter numbers (that is, you do not need to check if a user enters text).
Use a loop to iterate on a variable, i, from 2 through n. For each iteration, check all numbers from 2 through i to determine whether the number is prime. If it is prime, print out i and the word "Prime". Use the modulus operator, %, to determine if a number is prime
Example output:
Please enter a number larger than two: -939 Please enter a number larger than two: 2 Please enter a number larger than two: 20 2 Prime 3 Prime 5 Prime 7 Prime 11 Prime 13 Prime 17 Prime 19 Prime
This is what I have so far. I suppose i'm not really understanding how to continue with the second for loop. The first one just prints out a list of numbers from 2 to the number designated by the user. In my case that variable is user_input.
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;
I am trying to find the largest prime factor of a number. But when I am trying to determine if a number is a prime number in the function:
int is_prime(int number), I am unable to exit the loop.
Here is the code:
#include<iostream> using namespace std; int is_prime(int number) //the problem is in this function { int num = number; int factor=0; do{ num++; for(int i=1;i<(num+1);i++){
[code].....
So when the program runs, it first divides 20 by 2, to get 10, then divides 10 by 2 to get 5. Since, // condition 1 is not met, it passes 2 to the function int is_prime(int number). The function is able to return 3, but cannot exit the loop when num is 4.
I think the problem lies in the function: int is_prime(int number).
The recursive function is bolded, i got feedback and was told that the static variable made the function seem a lot like a iterative function but he did not say why.
Code: #define MAX 100 #include <string.h> #include <stdio.h> int checkPalindrome(char string[MAX]); int checkRecPalindrome(char string[MAX]);
the functions checks if the word is a palindrome like"level" "madam" etc. but with input "dfdfdfdffdfd" my recursive function fails.
Code:
/* main.c - Created on: Nov 9, 2013 - Author: Kaj P. Madsen*/ #define MAX 100 #include <string.h> #include <stdio.h> int checkPalindrome(char checkString[MAX]); int checkRecPalindrome(char checkString[MAX], int strLgt, int a); }
[code]....
results from "dfdfdfdffdfd" added some print to see that its the variables a and strLgt not functioning properly
Code:
dfdfdfdffdfd. The word is not a palindrome(iterative) strLgt: 11 a: 0 a: d strLgt: dstrLgt: 10 a: 1 a: f strLgt: fstrLgt: 9 a: 2 a: d strLgt: dstrLgt: 8 a: 3 a: f strLgt: fstrLgt: 7 a: 4
I am just practicing some recursion and I am having trouble with printing out a recursive function in main. Here is what I have:
Code:
// This function adds the squares 4, 5 = 4*4 + 5*5 recursiveley int recursive_sumSquares(int m, int n) { if (m < n) { return m*m + recursive_SumSquares(m+1, n); } else { return m*m;
[Code]...
I am getting an error that says undefined reference to 'recursive_SumSquares'
I am working on a problem that requires a nest for loop to be converted to a recursive function. I am going to provide just the code instead of the entire program.
Code:
for (R1=1; R1 <+3, R1++){ //for loop printf (something); } // the recursive function void loopR1 (int R1, int max){ if (R1 <= max){ printf (something);
[Code]...
when calling the recursive function in main i am using the following statement...