#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Exclamation Prime numbers in Scheme


    Okay guys, I'm trying to do a program that checks if a given number is prime or not.

    So far what I got:

    - Even numbers are not prime so I need check if a given number is even or not;
    - If it is, I give "#f", if it isn't I need to divide it by 2, if the remainder is 0 I give "#f", if it's not 0 I divide it by the 3,..., until sqrt N where N is the given number.

    I'm not getting how I will pass that to the language itself...

    Can I do it recursively?

    I think I need to create a new variable that will add one everytime the remainder isn't 0...

    Would be glad if someone could help me.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Regardless of the language you use, The Sieve of Eratosthenes is an easy implementation.

    Code:
    define primes as list (2)
    
    define number = 3
    
    while program not halted OR number < pre defined ending condition {
      for prime in primes {
        if  number %mod% prime == 0 {
          increment number
          continue loop
        }
      }
      add number to primes
      increment number
    }
    
    print list of primes
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    There's another solution involving regular expressions, or you could also simply attempt to factor the number using modular division.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo