Thread: Calulation prime number, using loops

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    9

    Question Calulation prime number, using loops

    how would I write a while() loop that calculates the sum of all prime numbers from 1 - 50.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    First figure out how you would calculate a prime number, then put it in a loop that goes 50 times. Once you get those two things working, you can add in a sum calculation inside the while loop. Paper and pencil are your friend.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, based on the way you keep asking us for homework solutions without showing any effort yourself, I would guess that you are really asking how we would write a while() loop that calculates the sum of all prime numbers from 1 - 50.

    But if you really want to know how you would do it, I would say: by studying while loops, summing (adding) numbers, and how to check if a number is prime. Your textbook, class notes and tutorials (found via Google) are great resources. Then you combine those pieces of knowledge and try to write the program yourself.

    If you have trouble, you post your code here (in code tags), with specific questions, and we will help. But we wont give out free solutions, otherwise you wont learn anything.

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Here's a solution

    Code:
    #include <stdio.h>
    
    unsigned short seed[] = {
        509, 508, 506, 504, 500, 498, 494, 492, 488, 482, 
        480, 474, 470, 468, 464, 458, 452, 450, 444, 440, 
        438, 432, 428, 422, 414, 410, 408, 404, 402, 398, 
        384, 380, 374, 372, 362, 360, 354, 348, 344, 338, 
        332, 330, 320, 318, 314, 312, 300, 288, 284, 282
    };
    
    
    int main(void)
    {
        int i, sum = 0;
        
        for (i = 0; i < 50; i++)
            sum += seed[i]^511;
        
        printf("Sum of first 50 primes is %d\n", sum);
    
        return 0;
    }
    Seriously, don't use that. But come up with *something*.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    No one ever searches the forum clearly....~80% of the questions on this forum involve something about prime numbers

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by nonpuz View Post
    No one ever searches the forum clearly....~80% of the questions on this forum involve something about prime numbers
    The percentage is not THAT high, but it is certainly not insignificant. And you have demonstrated the phenomenon that 74.6% of statistics are made up on the spot.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with finding only prime numbers with for loops
    By AlmondSeason in forum C++ Programming
    Replies: 11
    Last Post: 02-19-2013, 12:51 AM
  2. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 01:44 AM
  3. Replies: 3
    Last Post: 03-29-2005, 04:24 PM
  4. Price Calulation Method
    By gogo in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2001, 07:47 AM

Tags for this Thread