Results 1 to 2 of 2

Math Help - Matlab prime number with no built in functions

  1. #1
    Junior Member
    Joined
    Feb 2009
    Posts
    29

    Matlab prime number with no built in functions

    1) Write a program (an m-file) to compute the number of prime numbers for every “century” (i.e., # primes in the range [1-100], in [101-200], in [201-300], etc.) up to 100000. Plot your results. Approximately what percentage of numbers seems to be prime? Is this a good figure for all centuries?


    Normally I could use primes, isprime or factor functions but my professor wants us to do it with loops, and conditional statements...


    What I'm thinking that this could be a nested for loop? Could someone help me form this script please.What formula would i need to find the number of prime numbers for every century
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Senior Member
    Joined
    Mar 2009
    Posts
    378
    Well, you could brute force it and check to see if a particular number is divisible by all the numbers up until that number. However, there are some tricks that allow one to skip a lot of numbers. First, if a number is even it is not prime, so you only need to check odd numbers. Also, you only need to check if a number is divisible by primes, so if you keep a list that will help. There are probably more shortcuts than that.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 0
    Last Post: September 24th 2011, 11:23 AM
  2. Replies: 1
    Last Post: March 7th 2010, 09:30 PM
  3. Replies: 1
    Last Post: September 2nd 2009, 08:31 AM
  4. Correctly built parenthesis expressions
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: May 26th 2008, 09:10 PM
  5. Number theory, prime number
    Posted in the Number Theory Forum
    Replies: 1
    Last Post: September 17th 2006, 08:11 PM

Search Tags


/mathhelpforum @mathhelpforum