primes

Prime number function in R

落爺英雄遲暮 提交于 2019-12-27 15:44:33
问题 I am trying to create a function to test if a given integer is a prime number, I tried using the following: tpn <- function(prime.num){ if(prime.num==2){ print("PRIME") } else { if(prime.num%%(2:(prime.num-1))!=0){ print("PRIME") } else { print("NOT PRIME") }}} This doesn't work, although I cant understand why. I am checking to see if the given number can be divided by any of the integers up to this number with no remainders. If it cant, then the number is prime. Another solution I found was:

Prime number function in R

*爱你&永不变心* 提交于 2019-12-27 15:44:08
问题 I am trying to create a function to test if a given integer is a prime number, I tried using the following: tpn <- function(prime.num){ if(prime.num==2){ print("PRIME") } else { if(prime.num%%(2:(prime.num-1))!=0){ print("PRIME") } else { print("NOT PRIME") }}} This doesn't work, although I cant understand why. I am checking to see if the given number can be divided by any of the integers up to this number with no remainders. If it cant, then the number is prime. Another solution I found was:

Check if number is prime number

混江龙づ霸主 提交于 2019-12-27 12:01:10
问题 I would just like to ask if this is a correct way of checking if number is prime or not? because I read that 0 and 1 are NOT a prime number. int num1; Console.WriteLine("Accept number:"); num1 = Convert.ToInt32(Console.ReadLine()); if (num1 == 0 || num1 == 1) { Console.WriteLine(num1 + " is not prime number"); Console.ReadLine(); } else { for (int a = 2; a <= num1 / 2; a++) { if (num1 % a == 0) { Console.WriteLine(num1 + " is not prime number"); return; } } Console.WriteLine(num1 + " is a

Check if number is prime number

好久不见. 提交于 2019-12-27 11:58:18
问题 I would just like to ask if this is a correct way of checking if number is prime or not? because I read that 0 and 1 are NOT a prime number. int num1; Console.WriteLine("Accept number:"); num1 = Convert.ToInt32(Console.ReadLine()); if (num1 == 0 || num1 == 1) { Console.WriteLine(num1 + " is not prime number"); Console.ReadLine(); } else { for (int a = 2; a <= num1 / 2; a++) { if (num1 % a == 0) { Console.WriteLine(num1 + " is not prime number"); return; } } Console.WriteLine(num1 + " is a

C - determine if a number is prime

半腔热情 提交于 2019-12-27 10:20:23
问题 I am trying to come up with a method that takes an integer and returns a boolean to say if the number is prime or not and I don't know much C; would anyone care to give me some pointers? Basically, I would do this in C# like this: static bool IsPrime(int number) { for (int i = 2; i < number; i++) { if (number % i == 0 && i != number) return false; } return true; } 回答1: OK, so forget about C. Suppose I give you a number and ask you to determine if it's prime. How do you do it? Write down the

Why do we only check up to the square root of a prime number to determine if it is prime? Can't we use cube root?

限于喜欢 提交于 2019-12-26 16:31:11
问题 If a number n can be written as axb and m=sqrt(n). Here n=m*m. We say we only need to check upto m because min(a,b)<=m. So cant we take cube roots? Suppose we take n=21, then n=1x3x7. But Cube root is 2. Why does this method fail? 回答1: Consider n = 143 = 11 * 13. The cube root of 143 is between 5 and 6. If you only test divisibility by the primes up to 6, you will not find either of the two factors of n and will mistakenly conclude that 143 is prime. 来源: https://stackoverflow.com/questions

Segmented Sieve of Erastothenes C++ SPOJ [duplicate]

我只是一个虾纸丫 提交于 2019-12-26 03:09:05
问题 This question already has answers here : Segmented Sieve of Eratosthenes? (6 answers) Efficient algorithm to get primes between two large numbers (10 answers) Closed 5 years ago . I'm aware this has been asked before, but I cannot fully understand how to implement Segmented Sieve of Eratosthenes. Problem The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a

Finding the primary numbers in an array of pointer

怎甘沉沦 提交于 2019-12-25 18:47:12
问题 This piece of code which works fine it tells you to enter a number, then it puts the number in a for loop and it checks if it's dividable by i, if true it prints not prime if not prints prime. #include <iostream> using namespace std; int main() { int x; cin >> x; bool f = true; for (int i = 2; i < x; i++) { f = false; if (i % x == 0) f = true; if (f) cout << "not primary"; else cout << "primary"; } } but when i convert it to an array like so: #include <iostream> using namespace std; int main(

Code Optimization - Generating Prime Numbers

怎甘沉沦 提交于 2019-12-25 17:07:24
问题 I am trying to write a code for the following problem: Input The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space. Output For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line. Sample Input: 2 1 10 3 5 Sample Output: 2 3 5 7 3 5 My code: def prime?(number) return false if number =

How to print a text notification beside prime fibonacci numbers?

无人久伴 提交于 2019-12-25 09:37:07
问题 This is an assignment from a grade 12 computer science class. The section of the assignment I am having difficulty with reads as follows: Determine which of the first 20 Fibonacci numbers are prime numbers. Put a “this is a prime” text notification in the printout from the Basic challenge. Store the FibPrimes in an array called FibPrimes. Here is what I have attempted: Near the bottom, I attempted to make a loop that would print the text notification "This is a prime" if the given FibNum