Matlab Finding Prime numbers w/o built in functions...

Discussion in 'Programmer's Corner' started by Judas543, Mar 24, 2010.

  1. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    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
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
  3. johndoe45

    Member

    Jan 30, 2010
    364
    1
    don't have the time to help you out.
    but it is the exact same as the dice one. just use


    r=1:100:99901;
    s=100:100:100000;

    for i=1:length(r)
    t=r(i):s(i);
    end

    %therefore going to be 1:100 , 101:200, 201:301 ...........99901:100000

    if z==2 || z==3 || z==5 || z==7 || z==11 || z==13 || z==17 || z==19 || z==23 || z==29 || z==31 || z==37 || z==41 || z==43 || z==47 || z==53 || z==59 || z==61 || z==67 || z==71 || z==73 || z==79 || z==83 || z==89 || z==97

    don't even think need a for loop
    unless want to use n!+-1
    for ex. 1!+1=2 , 2!+1=3, 2!-1=1, 3!+1=7 , 3!-1,=5
    just use first method.

    just if statements.

    and the count functions like in dice game
     
    Last edited: Mar 25, 2010
  4. johndoe45

    Member

    Jan 30, 2010
    364
    1
    NOT COMPLETE. "IF" STATEMENTS GOING TO HAVE TO DO SOME RESEARCH. CAN'T FIGURE OUT A WAY TO PUT PRIME AS A FUNCTION IN TERMS OF n-1 OR SOMETHING WITH n. MAYBE ITS IMPOSSIBLE!!!!!!!!!! BUT CAN ALWAYS USE NUMERICAL METHOD APPROACHES

    clc
    clear all
    close all

    gap=100; %gap between years
    q=1; %first year
    w=100000; %last year

    r=q:gap:w-gap+1;
    s=q+gap-1:gap:w;
    k=1:gap;

    for i=1:length(r)
    for j=1:gap
    t(i,k)=r(i):s(i); %this is vector 1-100, 101-200, 201-300, 301-400.... 99901-100000 "centuries"
    count_a(i,j)=0; %counting variable
    if t(i,j) == 2 || t(i,j) == 5
    count_a(i,j)=count_a(i,j)+1; %counts primes in vector form. to check a single century
    %type count_a(whatever century you want,k)
    end %if want to see sum of single century type count_a(whatever century,j)
    end
    end

    v=sum(count_a')
    %this is the vector as a 1 row 1000 columns summing all prime numbers in a century. of course
    %coulumn one (1-100 aka 1st century) is going to have 2 prime numbers and everything else zero
    %cause of the "if" statement being only 2 and 5 if want to see how anything works just take off
    %the ; at end of a function
    plot(1:length(r),v)
    xlabel('centuries (years)')
    ylabel('prime numbers')
    title('Prime Numbers in a Century')
     
    Last edited: Mar 25, 2010
  5. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    This what I found which makes sense, but I can't figure out a way to do it by every 100 years? Unless it already does it


    clear
    clc

    n = 25;
    idx = true(1,fix(n/2));

    for ii = 3:2:sqrt(n)
    if idx((ii+1)/2)
    idx(((ii^2 + 1)/2):ii:end) = false;
    end
    end

    isprimesValue = 1:2:n
    isprimes = isprimesValue(idx);
    isprimes(1) = 2
     
  6. johndoe45

    Member

    Jan 30, 2010
    364
    1
    here is method to count primes
    has nothing to do with the centuries though
    can shorten it with more loops
    your professor gave you a hard task unless there is an easy way that i'm not seeing.
    this should be like a final exam :D
    Should be happy with just this.

    THIS GENERATES AND COUNTS ALL PRIME NUMBERS FROM 1-168. NO MORE THAN THAT OR IT IS INCORRECT.


    clc
    clear
    all
    close all


    u=168;

    t=1:1:u;
    z=1:1:u;
    x=1:1:u;
    c=1:1:u;
    s=1:1:u;
    a=1:1:u;

    q=t*2;
    w=t*3;
    e=t*5;
    r=t*7;
    aa=t*9;
    p=t*11;

    t(q)=0;
    z(w)=0;
    x(e)=0;
    c(r)=0;
    s(aa)=0;
    a(p)=0;

    j=t(1:q-1:u);
    b=z(1:w-2:u);
    n=x(1:e-4:u);
    m=c(1:r-6:u);
    d=s(1:aa-8:u);
    v=a(1: p-10:u);


    for i=1:u

    if i<4
    j(i)=1;
    end

    if
    i <9
    b(i)=1;
    end

    if
    i<25
    n(i)=1;
    end

    if
    i < 49
    m(i)=1;
    end

    if
    i < 81
    d(i)=1;
    end

    if
    i < 121
    v(i)=1;
    end

    end

    k=v.*d.*m.*n.*b.*j;
    g=1:u;
    f=g.*k;

    for e=1:u
    if f(e)>0
    f(e)=1;
    end
    end

    o=f.*g;
    count=0;

    for y=1:u
    if o(y) == 1
    o(y)=0;
    end
    if
    o(y) > 0
    count=count+1;
    end
    end

    o
    count
     
    Last edited: Mar 25, 2010
Loading...