How do you find the first N prime numbers in python?
You only increment min
in the else
block, i.e., if min % c
is nonzero for all c
, i.e., if min
is prime. This means that the code won't be able to move past any composite numbers. You can fix this by unindenting min=min+1
one level so that it lines up with the for
and else
.
How do I generate the first n prime numbers?
In Ruby 1.9 there is a Prime class you can use to generate prime numbers, or to test if a number is prime:
require 'prime'
Prime.take(10) #=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Prime.take_while {|p| p < 10 } #=> [2, 3, 5, 7]
Prime.prime?(19) #=> true
Prime implements the each
method and includes the Enumerable module, so you can do all sorts of fun stuff like filtering, mapping, and so on.
How to create a loop that returns the first N prime numbers in a vector?
You could write a recursive function (a function that calls itself). In addition to the two arguments n
and all
, it would also take a counter i
and a (empty) vector of prime numbers primes
.
As long as the number of prime numbers found is less than n
, the function calls itself and with each time it calls itself, it would increase the counter variable i
. If i
is a prime number, it gets added to the primes
vector.
get_prime <- function(n, all = TRUE, i = 1, primes = c()){
if ( n <= 0) {
stop("Not a valid number")
}
if (length(primes) < n) {
if (i == 2L || all(i %% 2L:ceiling(sqrt(i)) != 0)) {
get_prime(n, all = all, i = i + 1, primes = c(primes, i))
} else {
get_prime(n, all = all, i = i + 1, primes = primes)
}
} else {
if (all) {
return(primes)
} else {
return(tail(primes, 1))
}
}
}
The results are:
get_prime(7, TRUE)
[1] 2 3 5 7 11 13 17
get_prime(7, FALSE)
[1] 17
Sieve of Eratosthenes for first n prime numbers
You should count only prime numbers, not all numbers.
Also the range of i
for looping should be corrected. The first prime number is 2
and the element arr[100]
is not available.
for (int i = 2, count = 0; i < 100 && count != n; i++) // don't increment count here
{
if (arr[i] == 0)
{
cout << i << '\n';
count++; // count a prime number here
}
}
Python program to store first N prime numbers in a list?
This will return array of count
primes:
def get_primes(count):
primes = []
n = 2
while len(primes) != count:
for i in range(2, n // 2 + 1):
if n % i == 0:
break
else:
primes.append(n)
n += 1
return primes
you can use it like so:
n=input("Enter the number of prime numbers to be displayed: ")
array_of_n_prime_numbers = get_primes(n)
print("The prime numbers are:", array_of_n_prime_numbers)
Related Topics
Convert JSON String to JSON Array in Rails
Calling/Applying Lambda VS. Function Call - the Syntax in Ruby Is Different. Why
Cleanest Way to Create a Hash from an Array
Puts Doesn't Print Stuff to Console
Paginate Multiple Models in Kaminari
How Does Rails Implement Before_Filter
How to Put Assertions in Ruby Code
Iterate Every Month with Date Objects
How to Get Request.Uri in Model in Rails
Simple_Form: Remove Outer Label for an Inline Checkbox with Label
Ruby Class with Static Method Calling a Private Method
Generate a Migration File from Schema.Rb
How to Use Nokogiri to Parse an Xml File
Ruby Remove Empty Lines from String
Getting Memory Usage of My Process from Osx Using Ruby