Skip to content

Instantly share code, notes, and snippets.

@Aravin
Last active November 5, 2016 18:16
Show Gist options
  • Save Aravin/e80a5dcc120f23b585816c23057d122c to your computer and use it in GitHub Desktop.
Save Aravin/e80a5dcc120f23b585816c23057d122c to your computer and use it in GitHub Desktop.
T = gets.to_i

def prime(input)
    flag = 0
    return puts "Not prime" if input == 1

    for n in 2..(Math.sqrt(input))        
        if input%n == 0            
            flag = 1
            break
        end
    end

    if flag == 0
        puts "Prime"
    else
        puts "Not prime"
    end
end

T.times do 
    input = gets.to_i
    prime(input)    
end

// Solution: // return false n is divisible by any i from 2 to n - Worst // Checks if n is divisible by any number from 2 to n/2 - Better // Checks if n is divisible by any number from 2 to sqrt(n). - Good - #for n in 2..(Math.sqrt(input)) // Check for primality using odd numbers from 3 to sqrt(n) - n is not prime if it is evenly divisible by some 'i' in this range

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment