[algogeeks] help to code
Write a program that accepts an input integer n, and calculates the number and sum of all the numbers between 1 and n (inclusive) that are NOT evenly divisible by ANY of the first 5 prime numbers (2,3,5,7,11). The program should print out a clearly labeled count and sum my code is : it is not giving correct result #include iostream #includeconio.h using namespace std; int main() { int userInteger = 0; cout Enter A Number endl; cin userInteger; // Ask For a number from the user if (userInteger 0) // Is the number valid? { int result = 0; int prime[5] = { 2, 3, 5, 7, 11 }; int a = 1, count = 0; while (a userInteger) // Looping to user's input { int b = 0; while (b 5) // Looping the prime numbers array { if (a % prime[b]) { result += a; // If Not evenly divisible by prime number at index 'b' count++; } b++; } a++; // Increment the counter } cout Numbers Not evenly divisible by 5 prime numbers: count endl; cout The Sum of numbers not evenly divisible by 5 prime numbers: result endl; } getch(); return 0; } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] help to code
Clarification : The number (count) is the number of elements between 1 and n which are not evenly divisible by 5 prime numbers and the result is the sum of all the numbers between 1 and n which are not evenly divisible by 5 prime numbers . Right??? For Example : if n=5 then count -2 (i.e 1 and 3 ) and result = 5 If I am wrong ,Please Correct me and give me the outputs expected Thank You Rajeev N B On Tue, Jul 5, 2011 at 12:17 PM, shiv narayan narayan.shiv...@gmail.comwrote: Write a program that accepts an input integer n, and calculates the number and sum of all the numbers between 1 and n (inclusive) that are NOT evenly divisible by ANY of the first 5 prime numbers (2,3,5,7,11). The program should print out a clearly labeled count and sum my code is : it is not giving correct result #include iostream #includeconio.h using namespace std; int main() { int userInteger = 0; cout Enter A Number endl; cin userInteger; // Ask For a number from the user if (userInteger 0) // Is the number valid? { int result = 0; int prime[5] = { 2, 3, 5, 7, 11 }; int a = 1, count = 0; while (a userInteger) // Looping to user's input { int b = 0; while (b 5) // Looping the prime numbers array { if (a % prime[b]) { result += a; // If Not evenly divisible by prime number at index 'b' count++; } b++; } a++; // Increment the counter } cout Numbers Not evenly divisible by 5 prime numbers: count endl; cout The Sum of numbers not evenly divisible by 5 prime numbers: result endl; } getch(); return 0; } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] help to code
val = 2*3*5*7*11 for(i = 0 to n-1) if(val%a[i] == 0) count++,sum+=a[i]; surender On Tue, Jul 5, 2011 at 10:10 PM, Rajeev Bharshetty rajeev.open.1...@gmail.com wrote: Clarification : The number (count) is the number of elements between 1 and n which are not evenly divisible by 5 prime numbers and the result is the sum of all the numbers between 1 and n which are not evenly divisible by 5 prime numbers . Right??? For Example : if n=5 then count -2 (i.e 1 and 3 ) and result = 5 If I am wrong ,Please Correct me and give me the outputs expected Thank You Rajeev N B On Tue, Jul 5, 2011 at 12:17 PM, shiv narayan narayan.shiv...@gmail.comwrote: Write a program that accepts an input integer n, and calculates the number and sum of all the numbers between 1 and n (inclusive) that are NOT evenly divisible by ANY of the first 5 prime numbers (2,3,5,7,11). The program should print out a clearly labeled count and sum my code is : it is not giving correct result #include iostream #includeconio.h using namespace std; int main() { int userInteger = 0; cout Enter A Number endl; cin userInteger; // Ask For a number from the user if (userInteger 0) // Is the number valid? { int result = 0; int prime[5] = { 2, 3, 5, 7, 11 }; int a = 1, count = 0; while (a userInteger) // Looping to user's input { int b = 0; while (b 5) // Looping the prime numbers array { if (a % prime[b]) { result += a; // If Not evenly divisible by prime number at index 'b' count++; } b++; } a++; // Increment the counter } cout Numbers Not evenly divisible by 5 prime numbers: count endl; cout The Sum of numbers not evenly divisible by 5 prime numbers: result endl; } getch(); return 0; } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] help to code
I think you are getting it wrong. Surender, your solution says that numbers divisible by all of the first 5 prime numbers will be taken into account whereas the question says that only the numbers *not* evenly divisible by *any* of the first 5 prime numbers are to be added. Shiv, you are making two mistakes: 1. you say n is also included but your code excludes it. 2. you are just checking if a number is not divisible by one of the 5 prime numbers. If it is not divisible by any one number, you add it to your result, whereas your language of the question suggests that it should be that numbers which are not divisible by any of the five numbers, i.e., not divisible by all the 5 numbers ( not the ones divisible by just ant one of the 5). Hope I am correct in my interpretation. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] help to code
If my interpretation is right, following should be the code. int main() { int userInteger = 0; cout Enter A Number endl; cin userInteger; // Ask For a number from the user if (userInteger 0) // Is the number valid? { int result = 0; int prime[5] = { 2, 3, 5, 7, 11 }; int a,b, count = 0; *for(a=1;a=userInteger;++a) // Looping to user's input { for(b=0;b 5;++b) // Looping the prime numbers array { if (a % prime[b] == 0) //if divisible by any number, just end it there break; } //break or end of loop will bring the control here if(b==5) //a was not divisible by any of the first 5 prime numbers { result+=a; ++count; } }* cout Numbers Not evenly divisible by 5 prime numbers: count endl; cout The Sum of numbers not evenly divisible by 5 prime numbers: result endl; } getch(); return 0; } As per my solution, Test Cases: 1) userInteger = 20 count = 4 Numbers will be: 1, 13, 17, 19 result = 50 2) userInteger = 35 count = 7 Numbers will be: 1, 13, 17, 19, 23, 29, 31 result = 133 Even numbers can never be there in this list as they are all divisible by 2. Bfore 169, only prime numbers can be included. Hope my interpretation of your question was correct -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.