C/C++ :: How To Find Sum Of Even Factors
Nov 4, 2013How to find the sum of even factors ????
View 1 RepliesHow to find the sum of even factors ????
View 1 Repliesfind 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]......
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.
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].....
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 RelatedI 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] .....
I want to make a simple program that will print out the factors of an integer. My program right now only outputs "The prime factors of 221 are 221, 221, 221, 221"... Where it should be "The prime factors of 221 are 1, 13, 17, 221"
#include <cstdio>
int factorsOf(int x);
//function
int main() {
int x = 221;
printf("The factors of 221 are ");
[Code]...
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] .....
I'm trying to write a program that will find all the factors and primes for a range of numbers. I have the inner loop working but I am having trouble writing the outer loop that will output the range of numbers instead of just finding the factors for one number.
int n1 = 0;
int n2;
int factor = 0;
cout << "Enter a starting number: ";
cin >> n1;
cout << "Enter a ending number: ";
[Code] ....
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].....
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] ....
I need to find S and how did you find S in N is 4
#include <iostream>
int main () {
int n,i,s;
cout<<"Put the number of n"<<endl;
cin>>n;
s=0
for (i=1;i<=n;i++)
s=s+i
cout<<"S is<<s<<endl;
return 0
}
I'm a beginner in C and system programming. I need to use multiple process and POSIX shared memory to find all primes between 0 and 100. My code compiles, but the result is not correct, it shows all the multiples of 3 as primes.My instructor also mentioned that the multi-process portion will fork() the appropriate number of child processes. The parent process will create a POSIX shared memory object to which the sub-processes will attach. I am confused about the things he said about parent process.why I'm not getting the right primes?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/mman.h>
[Code]....
I just got assigned a project in my C programming class. The objective is to find the min and max number out of a .txt file full of numbers on separate lines called "data.txt". I am pretty much stuck at this point. I don't know how to actually scan the file and print the min and max. Also, the list of numbers in the data.txt file contains hundred on hundreds of numbers. So I couldn't specify the exact amount of numbers listed in the file. I must also find the count number, sum, and the average of the given data, So far I have:
Code:
#include<stdio.h>
int main()
{
int counter=0;
int maximum=0, minimum=1;
int num_data_points, avg;
double max, min;
FILE *sensor;
sensor = fopen("data.txt", "r");
[Code]...
This is the first time I'm trying to program in C# and I'm having some trouble.I can't find a way to compile whatever I do.I don't think it's a problem of what I wrote but I might not have what is necessary.I tried: C:>csc fisrt.cs csc was not a valid command.
View 1 Replies View RelatedI am working on a homework lab in which we have to find all the subsets equal to a given sum from an array of numbers.For example, an array of 7 numbers (1, 3, 4, 6, 7, 10, 25) with a sum of 25 would be (1, 3, 4, 7) and (25)...We are supposed to use dynamic programming to solve this.Now using the code below that we went over in class (Sedgewick's subsetSum), I understand how to this finds the first subset that adds up to the sum given. What's stumping me is how to find multiple subsets.
Code:
main(){
// Get input sequence
int n; // Size of input set
int m; // Target value
int *S; // Input set
int *C; // Cost table
int i,j,potentialSum,leftover;
}
[code]....
In class the teacher said it would be mainly just modifying the code we went over in class.
when I try to run the program (new to VC++, I click the green arrow next to "Debug" in the toolbar, right) and I get a message box - code builds without errors - but the debugger says: "Unable to start program 'c:users/sal/documents/visual studio 2010/Projects/SFML App/Debug/SFML App.exe Cannot find file specified"...well, I go to the directory it says, and nothing is even in the Debug file.
View 10 Replies View Relatedhow to get this program to find the palindrome? Here is my program:
#include <iostream>
#include <string>
using namespace std;
using std::string;
[code].....
Would you use regex? (not sure how though)
Or, would you go through the hard way, and iterate through every word, until you discarded (n-1) words and reached the nth word?
The second wouldn't be so short and clear to one who reads the code, therefore i was wondering if there's a neater way.
Consider a map that has the following properties;
- the map is two dimensional
- the map is perfectly square with dimensions 10000000x10000000
- the map has an associated set of many features
- the map does not "wrap around"
Each feature on the map is described by a 2D coordinate in the range (0, 0) to (10000000, 10000000).
Find the most isolated feature on the map, where the "most isolated feature" is the feature that is furthest (largest Euclidean distance) from any other feature. Because the map does not "wrap around", this should be a direct distance across the map.
<----10000000---->
--------------- -
| A | |
| | |
| | 10000000
| | |
| B C | |
| E D | |
--------------- -
In the example above, A is the most isolated feature on the square map with edge length 10000000. Write a program that reads in many features from standard input, and outputs the name of the most isolated feature to standard output. The format of the input is the feature name, x coordinate and y coordinate separated by spaces.
Each feature is on a new line. There may be any number of features between 1 and 100000. The program should be fast, so the algorithm must be better than O(n^2).
Any of the following languages are fine - C++, Python (>=2.5), C#, Java. The program shouldn't require any third-party libraries other than the chosen language's standard libraries, and should be compilable and runnable on a modern Windows or Linux development environment of your choice (e.g. MS Visual Studio, gcc, Eclipse etc). You should submit your program source code and any necessary makefiles or project files required for compilation. If you write the code in C++, using C++11 features is fine.
I need to be able to find every possible permutation using all possible values of a char. But I have to make it be able to form permutations from a length of 1 to variable N. For example, if N=3, I need it to be able to come up with
0x00
0x01
.......
0x00 0x00
0x01 0x01
.......
0xff 0xff 0xfe
0xff 0xff 0xff
How could I do this. (I would like to avoid recursion, since N might be as large as 50 or 60 and using recursion would most likely cause a stack overflow)
How can I find each and every pixel in a circle so that I can do some operations on it.
View 6 Replies View RelatedI know how to find find ASCII value of given character, but I am not getting how to find ASCII value of given string. For example I want to find ASCII value of string "HELLO",so how to do that.
View 13 Replies View RelatedJust wondering about how to write extremely simple version of 'find' in C: It just lists the path names of the files in the specified directories and all subdirectories.For example,
Code:
$find_version .
./foo
./bar
./baz
./baz/other
[Code]....
I got a code written in Java. But, I gave up writing code in Java. The program written is supposed to find the maximum subsequence sum. It's originally like this.
Code:
private static int maxSumRec (int [] a, int left, int right)
{
if(left == right)
if(a[left > 0])
return arr[left];
[Code] .....
I turned it into C, add some elements (to generate random numbers and change some variables' names), and becomes like this
Code:
int maxSumRec (val, left, right)
{
int x;
long int arr[val];
srand ( time(NULL) );
for(x=0; x<val; x++)
[Code] .....
It fails to compile. What have I done wrong? And I keep wondering why in the original code there is left and right variables and their values are never assigned. My c compiler (I use codeblocks) keeps telling me that. Idk why. My friend who keeps it in Java says it is fine but he cannot explain how his program works. What *is* left and right actually?
The method doesn't work properly, the point of the code is to tell the minimum value in the given matrix "m", it's supposed to take the first value of the matrix store it in min_ and then compare it to every value of the matrix, if the value stored in min_ is bigger than the current value in the loop then it's stored as the new min_ and at the end it's supposed to print that minimum...
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3
void minimum (int mat[SIZE][SIZE]){
int r,c;
int min_;
printf("
[Code] ......