int checkdiv(int num)
{
int SqrtOfnumber,i;
SqrtOfnumber=sqrt(num);
int counter=1;
for(i=2;i<SqrtOfnumber;i++)
{
if(num%i==0)
counter++;
}
counter=counter *2;
if(i*i == num)
counter++;
return counter;
}
**In cases of 2 numbers have the same number of divisors the output should be the one with the smallest value
Input examples
2 \\ test cases
1 10
1000 2000
Expected Output
Between 1 and 10, 6 has a maximum of 4 divisors.
Between 1000 and 2000, 1680 has a maximum of 40 divisors.
The above code Output
Between 1 and 10, 10 has a maximum of 4 divisors.
Between 1000 and 2000, 1680 has a maximum of 38 divisors.
The function is returning false number of divisors while the rest of the code work corectly, How can I fix it?
the input is a number which I want to check how many divisors it have and the output in the number of divisors
The fix for the problem was use double for the SquarOfnumber
int checkdiv(int x)
{
double SqrtOfnumber;
SqrtOfnumber=sqrt(x);
int counter=1,i;
for(i=2;i<SqrtOfnumber;i++)
{
if(x%i==0)
counter++;
}
counter=counter*2;
if(i*i == x)
counter++;
return counter;
}
Related
My goal is to display the first list of prime numbers depending on user's input, when the user input 10, the program should display first 10 prime numbers which are 2 3 5 7 11 13 17 19 23 29. I'm thinking of having generated list from 2 which is the first prime number to 541 which is the 100th prime number then if the user input for example 100 then it will be equivalent to 2 upto 541. Will my algorithm works? currently I'm able to display prime numbers from 2 - 541 and stucked on implementing another loop for 1-100 input.
#include <iostream>
using namespace std;
int main() {
int N, counter, i, j, isPrime, n;
cout << "List of prime numbers from 1-100: ";
cin >> N; // if the user input 10 then the first 10 prime numbers are "2 3 5 7 11 13 17 19 23 29"
// list of prime from 2-541 (or first list from 1-100 prime numbers)
for (i = 2; i <= 541; i++) {
isPrime = 0;
for (j = 2; j <= i/2; j++) {
if (i % j == 0) {
isPrime = 1;
break;
}
}
if (isPrime == 0 && N != 101)
cout << i << " ";
}
}
Your current algorithm doesn't work, because N is never modified. If the user inputs 101 then nothing will be printed, because if (isPrime == 0 && N != 101) will always be false. If the user inputs anything else then it will always print the first 100 primes.
The key idea will be to count how many primes were found and break after the Nth prime. Or count down, how many primes we still need t ofind. The rough outline will be as follows:
for (i = 2; N > 0; i++) { // Loop as long as we need to find more primes
...
if(isPrime == 0) {
std::cout << ...
N--; // <- Found one prime, N-1 to go
}
}
We have given the number of iterations upto which the number of ranges should be taken as input. Like if the number of iterations(n)=2, we have to input 2 ranges. And then for each range we have to count and print the total number of integers which have exactly two divisors. Sample input/output format is given below the code.
This is my code
#include<iostream>
using namespace std;
int main() {
long long int n, a, b, count=0, j, k, flag=1, ans=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a>>b;
for(j=a;j<=b;j++){
if(j==1){
continue;
}
else if(j==2){
count++;
}
else{
for(k=1;k<=j;k++){
if(j%k==0){
flag=0;
}
}
if(flag==1){
ans++;
}
}
if(ans==2){
count++;
}
}
cout<<count;
}
return 0;
}
Input Format
The first line contains N , no of test cases. The next N lines contain two integers a and b denoting the range of country numbers.
Output Format
Output contains N lines each containing an answer for the test case.
Sample Input
2
1 10
11 20
Sample Output
4
4
My output
1
1
I want to find total factors of any number.
In number theory, factorization is the breaking down of a composite number into smaller non-trivial divisors, which when multiplied together equal the original integer. Your job is to calculate number of unique factorization(containing at least two positive integers greater than one) of a number.
For example: 12 has 3 unique factorizations: 2*2*3, 2*6, 3*4 . Note:
3*4 and 4*3 are not considered different.
I have attempted to find that but not getting exact for all.
Here is my code :
#include<iostream>
using namespace std;
int count=0;
void factor(int n,int c,int n1)
{
for(int i=n1; i<n ; i++)
{
if(c*i==n)
{count++;
return;}
else
if(c*i>n)
return;
else
factor(n,c*i,i+1);
}
return;
}
int main()
{
int num,n;
cin>>num;
for(int i=0 ; i<num ; i++)
{
cin>>n;
count=0;
factor(n,1,1);
cout<<count<<endl;
}
return 0;
}
Input is number of test cases followed by test-cases(Numbers).
Example : Input: 3 12 36 3150
Output: 3 8 91
I think you are looking for number of factorizations of a number which are unique.
For this I think you need to find the count of number of prime factor of that number. Say for
12 = 2, 2, 3
Total count = 3;
For 2, 2, 3 we need
(2*2)*3 ~ 4*3
2*(2*3) ~ 2*6
2*2*3 ~ 2*2*3
To solve this we have idea found in Grimaldi, discrete and combinatorial mathematics.
To find number of ways of adding to a number(n) is 2^(n-1) -1. For 3 we have...
3 =
1+1+1
2+1
1+2
Total count = 2^(3-1) -1 = 4-1 = 3
We can use analogy to see that
1+1+1 is equivalent to 2*2*3
1+2 is equivalent to 2*(2*3)
2+1 is equivalent to (2*2)*3
Say number of prime factors = n
So we have number of factorizations = 2^(n-1)-1
The code:
#include <stdio.h>
int power(int x, int y)
{
int prod =1, i ;
for(i=1; i<=y;i++) prod *= x;
return prod;
}
int main()
{
int number,div;
int count = 0, ti, t;
printf("Input: ");
scanf("%d",&t);
for(ti=1; ti<=t;ti++)
{
scanf("%d", &number);
div = 2;count = 0;
while(number != 0)
{
if(number%div!=0) div = div + 1;
else
{
number = number / div;
//printf("%d ",div);
count++;
if(number==1) break;
}
}
printf("%d ", power(2,count-1)-1);
}
return 0;
}
Using mod is really useful in attempting to factor:
for(int i = 1; i <= fnum; ++i){ //where fnum is the number you wish to factor
if(!(fnum % i)) ++count;
}
return count;
Of cross this is the number of factors, not unique factors, if you want the number of unique factors, you have to do some additional work.
The solution is to realize that of all permutations, precisely one is sorted. 2 * 4 * 7 * 3 gives the same result as 2 * 3 * 4 * 7. That means that when you've found one factor, you should not check the remainder for lower factors. However, you should check if the same factor appears again: 12 = 2 * 2 * 3. The sequence 2 2 3 is also sorted.
BTW, you should give your variables clearer names, or at least add some comments describing them.
I have to code a program that counts how many prime numbers are between 2 and "n".
The first input have to be the total number of tests and other ones have to be the "n" (number limit of the range of numbers to check).
The problem:
My inputs: 7 10 15 50 100 1000 10000 7
The right outputs for the inputs above: 4 6 15 25 168 1229 4
What my code outputs: 4 6 15 25 800 9800 4
My code:
#include <iostream>
using namespace std;
int f(int number){
int m=0,k=1;
for(k; k<=number; k++)
if(number%k==0)
m++;
if(m==2)
return true;
}
int main (){
int limit=0, counter=0, test=0;
bool n;
cin>>test;
for(int v=0; v<test; v++){
cin>>limit;
for(int i=2; i<=limit; i++){
n=f(i);
if (n==true)
counter++;
}
cout<<counter<<endl;
counter=0;
}
return 0;
}
You probably need to turn on warnings in your compiler. The function f returns a bool (not an int as declared) and does not do so unless the number of divisors of x is equal to two. This are fairly trivial mistakes that any decent C++ compiler should warn you about. Do not ignore warnings.
I am wondering I can get some help at debugging or spotting the error in my program. The objective is to obtain user input and then display primes from input to zero, greatest prime to lowest.
Problem is the output includes the user input which may or may not be a prime number in itself, and repeats primes several times :(
Also, I am wondering why 2 isn't included?
My code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int prime, division, input;
cout << "Please enter a number you wish to count down prime numbers from." << endl;
cin >> input;
for (prime = input ; prime >=2 ; prime-- )
{
for (division = 2 ; division < prime ; division++ )
{
if (prime % division == 0)
{
break;
}
else
{
cout << prime << " ";
}
}
}
return 0;
}
My output:
Please enter a number you wish to count down prime numbers from.
15
15 13 13 13 13 13 13 13 13 13 13 13 11 11 11 11 11 11 11 11 11 9 7 7 7 7 7 5 5 5 3
Thanks for those who help!
This is a program which tells whether the input number is prime or not.Any number is completely divisible by a number which is always less than it.
In case of prime numbers they are completely divisible by 1 and itself;
So i have used a counter variable which counts how many times a number is completely divisible by a number or numbers less than it.
The count will be always 2 for prime numbers and count will be more than two for others.
Count will be 1 for one...
So the program is as follows....
#include<iostream.h>
#include<conio.h>
class prime
{
int a;
public:
void check();
};
void prime::check()
{
cout<<"Insert a number";
cin>>a;
int count=0;
for(int i=a;i>=1;i--)
{
if(a%i==0)
{
count++;
}
}
if(count==1)
{
cout<<"\nOne is neither prime nor composite";
}
if(count>2)
{
cout<<"\nthe number is not prime its composite" ;
}
if(count==2)
{
cout<<"\nthe numner is prime";
}
}
void main()
{
clrscr();
prime k;
k.check();
getch();
}
To print all the prime numbers less than the number given as input; code is as follows:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class prime
{
int a;
int i;
public:
void display();
};
void prime::display()
{
cout<<"Enter any number to find primes less than it";
cin>>a;
int count=0;
for(int j=a;j>=1;j--)
{
for(int i=1;i<=j;i++)
{
if(j%i==0)
{
count++;
}
}
if(count==2)
{
cout<<"\n"<<j;
}
count=0;
}
}
void main()
{
clrscr();
prime k;
k.display();
getch();
}
Try this code
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int prime, division, input;
cout << "Please enter a number you wish to count down prime numbers from." << endl;
cin >> input;
for (prime = input ; prime >=2 ; prime--)
{
boolean isDivisible = false;
for (division = 2 ; division < prime ; division ++)
{
if (prime % division == 0)
{
isDivisible = true;
}
}
if (isDivisible == false)
{
cout << prime << " ";
}
}
return 0;
}
For the first problem:
for 9, when division == 2, 9 % 2 ==1, so 9 is printed.Rather than You should have a flag value to denote if a number is prime or not, and print the number after you are sure it is prime.
For 2, EDIT: when prime == 2, and because division < prime is the condition that the inner loop get executed, the inner loop exits without running. All credits go to Marlon!
Check your inner for loop. Each run through the loop, your initializer gets reset to 2. Then you do Mod 2 on the suspected prime. If you get 0, all you really know is that the number is even. That might lead you to quickly say that the number isn't prime, but 2 mod 2 is 0, and 2 is prime. After the loop, "division" becomes 3. 15 mod 3 is 0. At that point, you break out of the loop, but 15 is NOT prime. Check your algorithm for determining primes. You could have it check against known primes, but that's not dynamic enough. There are other methods, one of which revolves around determining the suspected prime number's square root. Finally, you could do some good old (long) paper-pencil debugging.
Your inner loop is not correct. Its not printing primes, just odd numbers. It checks whether a number is divisible by 2 during first iteration and an even number will always be divisble. So the inner loop always breaks out if the number is even but it prints the number if odd and continues doing so till the loop breaks or terminates.
Lets try to get this with an example, the outer loop is at 9. The inner loop will check if its divisble by 2, since its not it'll print out the number and continue again. Next iteration will check whether its divisible by 3, since it is it'll break out.
And try using a function to check whether a number is prime that makes it more modular. Here's a little optimized version...
bool prime(int num)
{
int root = sqrt(num);
if(num == 2)
return true;
if(num%2 == 0)
return false;
for(int i=3;i<=root+1;i=i+2)
if(num % i == 0)
return false;
return true;
}