## 0 Replies - 1435 Views - Last Post: 20 October 2009 - 09:36 PM

### #1 gbertoli3 Reputation: 41
• Posts: 1,166
• Joined: 23-June 08

# Check if a Number is Prime

Posted 20 October 2009 - 09:36 PM

Description: # ::::DO NOT USE:::: if IsPrime(SomeNumberGoesHere)==true end # PROPER USE: # Check if 1 is a prime number if IsPrime(1) == "The number 1 is neither prime nor composite" # Some code goes here end # Check if 2 is a prime number if IsPrime(2) == "true" # Some code goes here end # Check if 4 is a prime number if IsPrime(4) == "false" # Some code goes here end Check if a Number is a Prime Number.
```# Check if a number is a Prime Number
# Original code came from shadhin on Dream.In.Code
def IsPrime(num)
# Placeholder for our result text to be returned
prime = "false"
# Placeholder for our factor
factor = 1
# If the number is equal to 1
if num == 1
# The number 1 is not a prime or composite number
prime = "The number 1 is neither prime nor composite"
# If the number is not equal to 1
else
# Loop through all the numbers in between 1 and 1 less than our number
for i in 1..(num-1)
# If the number % of i is equal to zero
if num%i==0
# Set our factor to i
factor=i
# Exit the if statement
end
# Exit the for loop
end
# If our factor is greater than 1
if factor>1
# This is not a prime number
prime = "false"
# If our factor is equal to 1 or below
else
# This is a prime number
prime = "true"
# Exit the if statement
end
# Exit the if statement
end
# Return our result
return prime
# Exit the method
end
```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }