Results 1 to 3 of 3

Math Help - Analysis of algorithms

  1. #1
    Newbie
    Joined
    Dec 2008
    Posts
    6

    Analysis of algorithms

    hello,
    i have problem with show correctness of these 2 algorithms:

    alg1

    prime(n)
    q:=2; results:=true;
    while (q< n and results) do
    begin
    if n mod q=0 then results:=false;
    q := q + 1;
    end;
    if results then "prime number"
    else "number isn't prime"

    ----------------------------------

    alg2 (Factorial n!)

    sil(n)
    begin
    sil := 1;
    m := n;
    while m > 0 do
    begin
    sil := sil*m;
    m := m-1;
    end;
    end
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member Greengoblin's Avatar
    Joined
    Feb 2008
    From
    UK
    Posts
    182
    I'm not sure what code that is, but in c syntax the factorial would be something like:

    Code:
    int n;
    scanf("%d",&n);
    
    int i,fact=1;
    
    for(i=n;i=1;i--){
      fact*=n
    }
    
    printf("%d",fact);
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Dec 2008
    Posts
    6
    yes, these algorithms weren't write in c++. It is pseudocode. I have to show correctness these two algoritms using for example induction or Loop invariant ( Loop invariant - Wikipedia, the free encyclopedia ). It isn't important what code is this. It may be write in pascal, c++, java etc. For example we have here File:FlowchartExample.png - Wikipedia, the free encyclopedia factorial algoritms... but what is the proof that this algoritms is correct? I search proof for my two algoritms: factorial and prime number. I don't know how to do that. I have to do it for subject Analysis of algorithms
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. algorithms
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: March 16th 2010, 02:25 PM
  2. Algorithms
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: December 17th 2009, 07:11 AM
  3. Algorithms
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: December 4th 2009, 03:06 AM
  4. Algorithms
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: March 4th 2009, 11:33 AM
  5. Algorithms!
    Posted in the Discrete Math Forum
    Replies: 4
    Last Post: December 15th 2006, 03:03 AM

Search Tags


/mathhelpforum @mathhelpforum