1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Helen [10]
3 years ago
11

Create a program that will output the sum of the prime numbers ( 2 - 100). A prime number is a number greater than 1 that cannot

be formed by multiplying two smaller natural numbers. some prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37... Example: Find the total sum of prime numbers from 1 - 100 The total sum is 1060. Verify your answers.
Computers and Technology
1 answer:
ruslelena [56]3 years ago
3 0

Answer:

The attached file is a C program that does much of what you want.  It does not output the the sum of the primes, but lists the primes.  The output is simple enough to change.  This program builds quite easily with GCC.

I don't know what language you're using, so I'm just providing a piece of code I've written that might help you out.

Also:

There are a few tricks in searching for prime numbers.  Here are some that I always like to take into account.

1) With the exception of 2 and 3, all primes are one away from a multiple of six

2) When testing a number for divisibility, you only have to test up as far as its square root.

3) You don't need to test for divisibility by composite numbers.  Only prime factors are needed. Any composite numbers smaller than x will be multiples of primes smaller than x.

So you can very rapidly find primes with a loop that starts at 6, and goes up to whatever limit you want, incrementing by 6. Let's call the number of  that loop x. You then only have to test if the x - 1 is prime and if x + 1 is prime.  That eliminates 2/3 of the iterations needed. Also, when you find primes, put them in an array.  I would start by creating an array with the numbers 2 and 3 already in it (those being the only two primes that are not adjacent to a multiple of six). Then each number tested only needs to be checked for divisibility by numbers in that array that are less than or equal to the square root of the number being tested.  If the number is composite, then it is only divisible by something larger than its square root if it is also divisible by something smaller than its square root.

With all of this big blurb written, and some hard-to-read C code attached, if you're just looking to write something quick to do your homework, and don't care about it being efficient, you can just do something like this javascript style example:

var n, tally = 0, list = [], isPrime;

for(n = 2; n < 100; n++){

 isPrime = 1;

 for(m = 0; m < list.length && list[m] <= Math.sqrt(n) && isPrime; m++){

   if(n % list[m] == 0) isPrime = 0;

 }

 if(isPrime){

   list[list.length] = n;

   tally += n;

 }

}

console.log('sum of primes: ' + tally);

I haven't run that code, so do watch out for any errors I missed.  Good luck!

Download txt
You might be interested in
Leonardo is having difficulty accessing the course website. he should contact the for assistance. (points:1)
Alona [7]
He should contact the teacher for assistance
3 0
3 years ago
If you receive an increase in pay, how will that affect your payroll deductions?
spin [16.1K]
No it will actually help it
3 0
4 years ago
C source files natively compiled on a Linux system using an ARM processor produce an executable file only runnable on ARM machin
Bumek [7]

Answer:

b. False.

Explanation:

Linux system is an operating system just like windows. Android is powered by Linux which is one of the most popular operating systems in Smart phones. Linux operating system was released in 1991, but it gained significance in 20th century. ARM processor can produce executable file on ARM machine with Linux system using x86 chipset.

3 0
3 years ago
Why do you think you are ready to handle the high school type of education and atmosphere? (in complete sentences)​
sertanlavr [38]

Answer:

I'm already in high school but at first, I was scared for my life literally. It wasn't what I expected it to be, though. Everyone was super nice to me and has been ever since I started. Turns out, high school is amazing.

Explanation:

5 0
3 years ago
Question # 4
Valentin [98]

Answer:Control +c

Explanation:yes

3 0
3 years ago
Read 2 more answers
Other questions:
  • Database management systems _____. a. include transaction-processing reports for database analysis b. are used to create, organi
    6·1 answer
  • Enter a formula in cell b7 to calculate the average value of cells b2:b6
    13·1 answer
  • One example of how psychological research can be used to control harmful behavior would be __________. A. fooling individuals th
    13·2 answers
  • Karen thinks she should go out with Blane, an unattractive, social misfit, because “If I don’t go out with him, people will thin
    7·1 answer
  • Write a statement to add the key Tesla with value USA to car_makers. Modify the car maker of Fiat to Italy. Sample output for th
    9·1 answer
  • Pls help brainliest
    12·1 answer
  • A project team was researching the risks involved in setting up a computer system. They decide to go ahead with their plan, rega
    7·1 answer
  • Characteristics of hybrid computer​
    11·1 answer
  • Write a query that returns the first name of a student whose last name is "Patil" and is in the Ravenclaw house.
    9·1 answer
  • Why do digital media professionals create portfolios of their work?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!