I need compute the first 100 prime numbers, but in the output i got "9" and other in my numbers....................... i want compute the first 100 prime numbers
{
bool prime; int start, new, kor,k, i,gg;
start=1;
k=1 ;
gg=0;
do
{
if (start < 2) {new = 2;}
if (start == 2) {new = 3;}
if (start > 2) {
if ((new % 2) == 0)
new--;
do {
prime = true;
kor=sqrt(new);
new+=2;
for (i=3;prime&& (i<=kor); i+=2) {
if (new % i == 0)
prime=false;}
} while (!prime) ;
}
gg++;
printf("%d->%d\n",gg, new);
k++;
start++;
continue;
}
while (k<101);
}
With
if (start < 2) {new = 2;}
if (start == 2) {new = 3;}
you have special cases the first and second numbers.
Next time round the do...while loop we skip the for loop because kor is 1, thereby printing 5. Which we didn't check, so perhaps we just got lucky. Smells like we don't check far enough.
Next time, after
kor=sqrt(new1); new1+=2;
kor is 2, so again we don't do the for loop, and print 7. Next time we have the same situation. kor is still 2 so you get 9.
I think if you switch the new+=2 to before the kor=sqrt(1); it will work.
Once you are in this part, you don't need to check if something is even, since you always add 2 to an odd number.
BTW Why does it say continue as the last thing in the loop?
This might be better (I took the liberty of putting it in a function):
void find_primes()
{
bool prime; int start, new, kor,k, i,gg;
start=1; k=1 ;gg=0;
do
{
if (start < 2) {new = 2;}
if (start == 2) {new = 3;}
if (start > 2) {
do {
prime = true;
new+=2;
kor=sqrt(new);
for (i=3;prime&& (i<=kor); i+=2) {
if (new % i == 0)
prime=false;
}
}
while (!prime) ;
}
gg++; printf("%d->%d\n",gg, new);
k++;
start++;
}
while (k<101);
}
I don't know what language you're programming in, so I'm taking a guess at this (I can't comment yet)-
Are you getting all odd numbers as output in your answer? 9, 11, 13, 15...
It seems that you are printing out all values of 'new', regardless of whether prime is true or not. Maybe you should put a
if (prime) {
printf("%d->%d\n",gg, new);
}
there
here is an example program from:
http://www.programmingsimplified.com/c/source-code/c-program-for-prime-number
note: 1 is not a prime number, see:
http://primes.utm.edu/notes/faq/one.html
#include<stdio.h>
int main()
{
int n, i = 3, count, c;
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= (i - 1) ; c++ )
{
if ( i%c == 0 ) break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
return 0;
}
Related
Here is the code:
#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
// choose(n,m) returns how many ways there are to choose m items from
// a set of n items
// requires: 0 <= m, 0 <= n
int choose(int n, int m)
{
if (m == 0 || m == n)
{
return 1;
} // base case
// recursive step
return (choose(n - 1, m) + choose(n - 1, m - 1)); // continues until we have exhausted the
// number of possibilities to choose from, base case then terminates it
// taken from the assignment sheet
}
// num_divisors_up_to_k(n,k) returns the number of positive divisors
// of n that are less than or equal to k
// requires: 1 <= k <= n
int num_divisors_up_to_k(int n, int k)
{
if (k == 0)
{
return 0;
} // base case
if (n % k == 0) // if divisible by k return 1 and call recursively
{
// call function again moving to next number
return 1 + num_divisors_up_to_k(n, k - 1);
}
else // otherwise return 0 and call recursively
{
return 0 + num_divisors_up_to_k(n, k - 1); // next number
}
}
// is_prime(n) returns true if n is a prime number and false otherwise
// requires: 1 <= n
bool is_prime(int n)
{
if (n < 2) // 2 is the lowest prime number
{
return false;
}
for (int i = 2; (i * i) <= n; i++) // loop through each element
{
if (n % i == 0) // if divisible by i its not prime so return false
{
return false;
}
}
return true; // num is prime
}
// collatz(n) returns the number of steps it takes to reach 1 by
// by repeatedly applying the Collatz mapping on n; prints each
// number in the sequence starting at n
// requires: 1 <= n
int collatz(int n)
{
if (n == 1) // base case
{
return 0;
}
// print n
printf("%d ", n);
// even
if (n % 2 == 0)
{
// adding 1 and calling collatz again with next value
return 1 + collatz(n / 2);
}
else
{
// same thing as even just with the method for odd
return 1 + collatz(3 * n + 1);
}
}
int main()
{
// collatz code
int n;
printf("enter number:\n");
scanf("%d", &n);
while (n == 1)
{
// cant be negative or 0
if (n < 1)
{
printf("wrong input");
}
else
{
// else printing collatz sequence for input
printf("collatz(%d): ", n);
int steps = collatz(n);
// print out number of steps
printf("it took %d steps\n", steps);
}
// next input
printf("enter number: \n");
}
// TESTS CASES
// choose function
assert(choose(2, 1) == 2);
assert(choose(4, 2) == 6);
assert(choose(10, 10) == 1);
assert(choose(8, 2) == 28);
assert(choose(0, 0) == 1);
// num_divisors_up_to_k function
assert(num_divisors_up_to_k(9, 9) == 3);
assert(num_divisors_up_to_k(1, 1) == 1);
assert(num_divisors_up_to_k(100, 100) == 9);
assert(num_divisors_up_to_k(0, 0) == 0);
// is_prime function
assert(is_prime(10) == false);
assert(is_prime(2) == true);
assert(is_prime(7919) == true);
assert(is_prime(479001599) == true);
// collatz
assert(collatz(5) == 5);
assert(collatz(13) == 9);
assert(collatz(1) == 0);
printf("PASSED\n");
}
Code does not work in visual studio code but works in repl.it or any online complier. I pretty much get a runtime error on vscode and am forced to end the program myself. im not very good with the technicality of IDE's and I know this code doesn't work as intended but I can't fix it unless I can properly compile it and see the output. if anyone can help me my discord tag is ansh#1234 or you can reply here. looking forward to making this work and thank you in advance
Here is the output on VSCode:
Here is the output on repl.it:
There is configuration issue inyour Visual Studio Code setup. You should just run the program manually in a shell window by typing:
cd /Users/ansh/Desktop/c
gcc assignment1.c -o assignment1
./assignment1
#include <stdio.h>
main()
{
int n;
int x;
int h=24;
int s=9;
scanf("%d",&n);
while (n>0)
{
n=n-1;
scanf("%d",&x);
while (x<=9)
{
if (x==1)
printf("2\n");
if (x==2)
printf ("3\n");
if (x==3)
printf ("5\n");
if (x==4)
printf("7\n");
if (x==5)
printf("11\n");
if (x==6)
printf("13\n");
if (x==7)
printf("17\n");
if (x==8)
printf("19\n");
if (x==9)
printf("23\n");
break;
}
while (23<h<542 && x>9)
{
h=h+1;
if ( (h%2)!=0 && (h%3)!=0 && (h%5)!=0 && (h%7)!=0 && (h%11)!=0 && (h%13)!=0 && (h%17)!=0 && (h%19)!=0 && (h%23)!=0 )
{
s=s+1;
if (x==s)
printf("%d\n",h);
}
}
}
}
The question for the code is to enter n which will be the number of the following inputs. Each input must give the xth prime number.
example:
input:
3
,4
,20
,50
output:
7
,71
,229.
x can be between 1 and 100. (the first 100th prime numbers)
Now my problem is with x>9.after entering one value for it, it won't accept anymore values for x.
I would like to know why this happens and how to fix it.
I'm also very new to programming and haven't learned arrays yet.(I know scanfs aren't the best thing but that's all I've learned so far)
This line:
while (23<h<542 && x > 9)
is creating an infinite loop when x is greater than 9. 23 < h < 542 doesn't test whether h is between 23 and 542. That expression is equivalent to (23 < h) < 542. (23 < h) is 1 if 23 is less than h, which is always the case because h starts as 24 and the loop increases it. And 1 is always less than 542.
What you want is:
if (x > 9) {
while (h < 542)
...
}
}
There's no need to test x each time through the loop, because it never changes within the loop. And there's no need to test 23 < h, because that's always true.
When you do need to check whether a variable is between two numbers, the way to do it is with 23 < h && h < 542.
Another problem is that when you enter multiple numbers higher than 9, you're not resetting h and s back to their initial values before the while loop that looks for higher primes. You should initialize those values right before the loop, not just at the top of the program.
You can also make use of cascaded if/else if/else so you don't need to test x at the end (or you could use switch/case).
Here's the full rewritten program:
#include <stdio.h>
main() {
int n;
int x;
scanf("%d",&n);
while (n>0) {
n=n-1;
scanf("%d",&x);
if (x==1) {
printf("2\n");
} else if (x==2) {
printf ("3\n");
} else if (x==3) {
printf ("5\n");
} else if (x==4) {
printf("7\n");
} else if (x==5) {
printf("11\n");
} else if (x==6) {
printf("13\n");
} else if (x==7) {
printf("17\n");
} else if (x==8) {
printf("19\n");
} else if (x==9) {
printf("23\n");
} else {
int h = 24;
int s = 9;
while (h<542) {
h=h+1;
if ( (h%2)!=0 && (h%3)!=0 && (h%5)!=0 && (h%7)!=0 && (h%11)!=0 && (h%13)!=0 && (h%17)!=0 && (h%19)!=0 && (h%23)!=0 ) {
s=s+1;
if (x==s) {
printf("%d\n",h);
}
}
}
}
}
}
DEMO
How I can improvise the below code find the prime no.s from an array whose index is also a prime no.
This is my basic code :
#include <stdio.h>
main() {
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);
/*logic*/
for (i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}
if (c == 2) {
printf("n is a Prime number");
}
else {
printf("n is not a Prime number");
}
return 0;
}
The code you provided can be optimized like this (It doesn't work for 2 and 3 though).
bool is_prime( int n )
{
if ( n % 2 == 0 || n % 3 == 0 ) {
// If multiple of 2 or 3 it's not a prime
return false;
}
// Search for divisors from 5 to only sqrt(n)
// Example: 36: (1*36) (2*18) (3*12) (6*6) (12*3) (18*2) (36*1)
for ( int i = 5; i * i <= n; i += 6 ) {
// Check only odd numbers
if ( n % i == 0 || n % ( i + 2 ) == 0 ) {
// i+4 is not checked cause it's a multiple of 3
// i is increased by 6 (multiple of 3) and the first i+4 = 9
return false;
}
}
return true;
}
You use the above function to find and store the prime numbers which are less or equal to the prime size of the array. Then for each found prime p you check if array[p] is prime with the same function.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a project yet my teacher hasn't taught us about arrays. We need to output =<> signs corresponding to the comparison of one number to another. IE the main number is 1234 and I put in 2315, the output would be <<<> where the signs do not go in the order of the numbers but by this order =, <, >.
I have an idea and that to use an array then to use some code that would read out the whole array and apply rules to it, however I do not know how to implement this. I have been googling for awhile now and nothing I found really helps.
Just to let you know the program has way more steps than just this, all of which I have already completed, I just can't figure out this part. I do not want just the answer, I just want someone to point me in the right direction.
Thanks
EDIT:: The example 1234 and 2315 are bad examples. To give a more definitive idea without giving away too much of the problem so I have work to do is listing num1 and num2 (corresponding to 1234 and 2315) from least to greatest or greatest to least and compare that way. So another example would be 4751 is the main number and I put in 1294. The output would be ==<>. Thanks for the help guys so far. I am learning a lot.
EDIT2:: Thanks guys for the help. I learned a lot. I don't want any more submissions at least until I can upload my code.
Taking you at your word that you've already successfully completed most of your assignment, and by giving you code that you'll have to work through and understand to figure it out and adapt it to your needs, this will do what you want. The fact that you don't have to output the signs in the same order as the numbers themselves is what makes this easier.
#include <stdio.h>
int main(void) {
int num1 = 1234;
int num2 = 2315;
int lt = 0, gt = 0, eq = 0;
while ( num1 > 0 && num2 > 0 ) {
int op1 = num1 % 10;
int op2 = num2 % 10;
if ( op1 < op2 ) {
++lt;
} else if ( op1 > op2 ) {
++gt;
} else {
++eq;
}
num1 /= 10;
num2 /= 10;
}
for ( int i = 0; i < eq; ++i ) {
putchar('=');
}
for ( int i = 0; i < lt; ++i ) {
putchar('<');
}
for ( int i = 0; i < gt; ++i ) {
putchar('>');
}
putchar('\n');
return 0;
}
and outputs:
paul#MacBook:~/Documents/src/scratch$ ./eq
<<<>
paul#MacBook:~/Documents/src/scratch$
This code lets you get the nth digits you can compare and make a count of each symbol you need to return
char nthdigit(int x, int n)
{
while (n--) {
x /= 10;
}
return (x % 10) + '0';
}
And this is how you get the length of a number, check this post
As promised here is the rest of my code. It fixes the issue pointed out in the question but I have another issue. It is probably pretty noticeable but I wanted to post my code so I don't forget again.
#include<stdio.h>//standard inputs and outputs
#include<stdlib.h>//for compare, qsort, srand, and rand function
#include<time.h>//for random numbers at different times
#include<unistd.h>//for fun at the end
int main(void){
int guess, gdig1, gdig2, gdig3, gdig4, random, rdig1, rdig2, rdig3, rdig4;//variables for main
int lt, gt, eq, i, q, temp, holder;//this is for the hints
int g[3],r[3];
printf("Give the computer a few seconds to come up with a super secret passcode.\n");
do{//figuring out a random code
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);//using an unassigned integer
random = rand()%9000+1000;//generating a random number but limiting it
rdig4 = random%10;
rdig3 = (random/10)%10;
rdig2 = (random/100)%10;
rdig1 = (random/1000)%10;//figuring out the individual digits of the code
} while ((rdig1 == rdig2)||(rdig1 == rdig3)||(rdig1 == rdig4)||(rdig2 == rdig3)||(rdig2 == rdig4)||(rdig3 == rdig4)||(rdig1 == 0)||(rdig2 == 0)||(rdig3 == 0)||(rdig4 == 0));
//^^ some crazy boolean expression making sure the random integer doesnt have any of the same digits.
printf("\nThe actual passcode is:%d.\n",random);//testing in beginning comment out ********
do{
do{
printf("\nEnter in your guess for the passcode: ");
scanf("%d",&guess);//inputting and reading the guessed code
// printf("You entered:%d\n",guess);//just to check comment out at end**
gdig4 = guess%10;
gdig3 = (guess/10)%10;
gdig2 = (guess/100)%10;
gdig1 = (guess/1000)%10;//figuring out the individual digits of the guess code using modulus operator
if (guess > 9999){//the starting loop statement to make sure number is valid. this one is if it is greater than 4 digits
printf("\nPlease use a four digit number for the passcode.\n");
}
else if (guess < 1000){//this one is if it is less than 4 digits
printf("\nPlease use a four digit number for the passcode.\n");
gdig1 = 1;
gdig2 = 1;//used so the computer still loops
gdig3 = 1;
gdig4 = 1;
}
if ((gdig1 == 0) || (gdig2 == 0) || (gdig3 == 0) || (gdig4 == 0)){
break;
}
if ((rdig1 == gdig1) && (rdig2 == gdig2) && (rdig3 == gdig3) && (rdig4 == gdig4)){//to skip this codeblock and move onto next
break;
}
if (guess > 9999){
break;
}
if (guess < 1000){
break;
}
printf("\n%d %d %d %d\n",rdig1,rdig2,rdig3,rdig4); //used to testing comment out at end
printf("\n%d %d %d %d\n",gdig1,gdig2,gdig3,gdig4);
while (guess > 0){
g[i++] = guess % 10;
guess /=10;
}
do{//took a long long LONG time to get
for(i = 0; i<3;i++){
if(g[i] > g[i+1]){
holder = g[i+1];
g[i]=g[i+1];
g[i+1] = holder;
}
}
}while (i == 1);
for(i = 0;i<4;i++){
printf("%d",g[i]);
}
while (random > 0){
r[i++] = random % 10;
random /=10;
}
do{//took a long long LONG time to get
for(i = 0; i<3;i++){
if(r[i] > r[i+1]){
temp = r[i+1];
r[i]=r[i+1];
r[i+1] = temp;
}
}
}while (i == 1);
for(i = 0;i<4;i++){
printf("%d",r[i]);
}
/* for(digit=0;digit<4;digit++){
for(tmp=guess;tmp>0;tmp/=10){
if(tmp%10==digit){
printf("%d",digit);
g[i++]=digit;
}
}
}
printf("\n");
for(i=0;i<4;i++){
printf("%d",g[i]);
}//just to check
//this is for sorting the random
for(digit=0;digit<4;digit++){
for(tmp=random;tmp>0;tmp/=10){
if(tmp%10==digit){
printf("%d",digit);
r[i++]=digit;
}
}
}
for(i=0;i<4;i++){
printf("%d",r[i]);
}//just to check
*/
//this is for hints
rdig1=r[0];//redefining the random and guess digits so it is easier later
rdig2=r[1];
rdig3=r[2];
rdig4=r[3];
gdig1=g[0];
gdig2=g[1];
gdig3=g[2];
gdig4=g[3];
q = 0;
eq = 0;
lt = 0;
gt = 0;
if (random > 0){//loop that always holds true
if (gdig1 == rdig1){
eq++;
}else if (gdig1 < rdig1){
lt++;
}else if (gdig1 > rdig1){
gt++;
}
if (gdig2 == rdig2){
eq++;
}else if (gdig2 < rdig2){
lt++;
}else if (gdig2 > rdig2){
gt++;
}
if (gdig3 == rdig3){
eq++;
}else if (gdig3 < rdig3){
lt++;
}else if (gdig3 > rdig3){
gt++;
}
if (gdig4 == rdig4){
eq++;
}else if (gdig4 < rdig4){
lt++;
}else if (gdig4 > rdig4){
gt++;
}
}
for (q = 0; q < eq; q++){//counting step for the = <> no problems here^^
putchar('=');
}
for (q = 0; q < lt; q++){
putchar('<');
}
for (q = 0; q < gt; q++){
putchar('>');
}
//everything below is correct do not mess with *******************************************************************************************
} while (gdig1 > 0);//to loop inputs until they input correctly
if ((gdig1 == 0) || (gdig2 == 0) || (gdig3 == 0) || (gdig4 == 0)){//a nested if statement to check each digit if it is a 0
printf("\nYou have entered the Super Secret Code!!!!!\nThe actual passcode is:%d.\n",random);
break;
}
if ((rdig1 == gdig1) && (rdig2 == gdig2) && (rdig3 == gdig3) && (rdig4 == gdig4)){//to skip this codeblock and move onto next
break;
}
} while (((rdig1 != gdig1) && (rdig2 != gdig2) && (rdig3 != gdig3) && (rdig4 != gdig4)));//to loop inputs until they got it
//everything below is correct do not mess with *******************************************************************************************
if ((rdig1 == gdig1) && (rdig2 == gdig2) && (rdig3 == gdig3) && (rdig4 == gdig4)){
printf("\nCorrect Code inputted.");
sleep(1);
printf("\nImplementing unlocking procedures...\n");
for (i=0;i<=4;i++){//for fun cause why not. if you spend hours on code might as well have some fun :)
int p =25*i ;
printf("%d%% complete................\n",p);
sleep(1);
}
printf("Stand back!!! The vault is now opening.\n");
}
return 0;
}
I need to write a recursive function which accepts a number, and it needs to check whether each number is divisible with the previous number. For example, if the input is 63542, result should be 2, while only 6 is divisible by 3, and 4 is divisible by 2. These are the only numbers that are divisible with the their previous number. I have the following code, but it returns every time one more. For the above example it should return 2 but it returns 3.
#include <stdio.h>
int deliv(int num)
{
int temp = num%100;
int counter = 0;
if(num == 0)
{
return 0;
}
else if((temp/10)%(num%10) == 0)
{
counter++;
return counter + deliv(num/10);
}
else return counter + deliv(num/10);
}
int main()
{
int result = deliv(63542);
printf("%d\n", result);
return 0;
}
You want to change your test condition from if(num == 0) to if(num < 10 ) because if num is a single digit number, temp becomes 0 since temp = num/10, which is divisible by num%10.
Also, add this condition to avoid crashing when two consecutive digits are 0.
if(num < 10)
{
return 0;
}
//To avoid crash due to 2 zeroes
else if(temp==0)
{
return counter + deliv(num/10);
}
else if((temp/10)%(num%10) == 0)
{
counter++;
return counter + deliv(num/10);
}
else return counter + deliv(num/10);