primes

Java BigInteger prime numbers

时间秒杀一切 提交于 2019-12-13 11:46:30
问题 I am trying to generate a random prime number of type BigInteger, that is between a min and max value which I supply. I am aware of the BigInteger.probablePrime(int bitlength, random), but I am not sure how or even if the bitlength translates into a max/min value of the outputted prime. Thanks, Steven1350 回答1: BigInteger.probablePrime(bitLength, random) is going to return a (probable) prime of the specified bit length. That translates into a maximum value of 2^bitlength - 1 and a minimum of 2

Prime number Logic, n/2 condition in a loop

徘徊边缘 提交于 2019-12-13 11:30:47
问题 The following code is for prime number. I want to know why we use i<=n/2 condition in the loop. C Program: #include <stdio.h> int main() { int n, i, flag = 0; printf("Enter a positive integer: "); scanf("%d",&n); for(i=2; i<=n/2; ++i) { // condition for nonprime number if(n%i==0) { flag=1; break; } } if (flag==0) printf("%d is a prime number.",n); else printf("%d is not a prime number.",n); return 0; } 回答1: Although this is C program. But prime number logic will be same for C and Java both

Python script to find nth prime number

北慕城南 提交于 2019-12-13 10:53:17
问题 I'm new to Python and I thought I'd try to learn the ropes a bit by writing a function to find the nth prime number, however I can't get my code to work properly. No doubt this is due to me missing something fundamental, but I'd appreciate your help in finding where it went wrong! c=2 n=input("Which prime would you like? ") n=int(n) a=[] l=len(a) while l<=n: if c==2: a.append(c) elif (c % 2 ==0): #c is even break elif (c % 2 !=0): #c is odd if c<7: a.append(c) elif c >=7: for i in range(3,int

I'm trying to find the sum of primes below 2 million in Java [duplicate]

穿精又带淫゛_ 提交于 2019-12-13 09:17:28
问题 This question already has answers here : Project Euler #10 Java solution not working (6 answers) Closed 6 years ago . I'm trying to find the sum of primes below millions. My code works when I try to find the sum of primes below hundred thousands but when I go large numbers it doesn't work. So I need some help to get this work for big numbers... import java.util.Scanner; public class sumPrime { public static void main (String args []){ long n = 2000000; int i; int j;int sum =0; for (i=2; i <n;

Euler 3 Python. Putting the prime numbers into a list

梦想的初衷 提交于 2019-12-13 07:43:12
问题 Im still pretty new to python and I'm trying to get all of the prime numbers from 600851475143 into a list. However, I keep getting a random assortment of numbers in the list instead of the prime numbers. I'm not really sure where I am going wrong. Thank you for your time import math factors_list = [] prime_factors = [] def number_factors(s): s = int(math.sqrt(s)) for num in range(2, s): for i in range(2, num): if (num % i) == 0: factors_list.append(num) else: prime_factors.append(num) number

Finding the 10001st prime - how to optimize?

China☆狼群 提交于 2019-12-13 05:54:24
问题 Project Euler problem 7 says: By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? Here's the code I've written in Python: def primes(n): primes = [] attempt = 3 while len(primes) < (n-1): for i in range(len(primes)): if attempt % primes[i] == 0: attempt += 2 break else: primes.append(attempt) print (primes) return (primes) While testing a number, if it finds that the number is divisible by one of the primes in

First 100 prime numbers

别等时光非礼了梦想. 提交于 2019-12-13 05:23:09
问题 I am a beginner in Python and I have a question regarding finding 100 prime numbers. I know there are a number of ways to do it but please help me in my approach. I find the value of count to be increasing but for some reason the while loop condition doesn't apply. from __future__ import print_function count = 0 while(count <= 20): for i in range(2,20): for j in range(2,i): if i < j: print("The number",i,"is prime") elif i % j == 0: break else: print("The number",i,"is prime") count = count +

C Libgcrypt: Unable to check if number is prime using libgcrypt

喜你入骨 提交于 2019-12-13 02:57:18
问题 I am using a libgcrypt function gcry_prime_check to test if the number 3 is a prime number. It turns out 3 is not a prime number according to my the function. What am i doing wrong? Here is my code #include <gcrypt.h> #include <stdio.h> int main(void) { gcry_mpi_t cript_prime; gcry_error_t err; char buffer[8] = {0}; char number[8] = {0}; printf("%s\n", gcry_check_version ( NULL ) ); gcry_control( GCRYCTL_INIT_SECMEM, 16384, 0 ); cript_prime = gcry_mpi_new(16); strcpy(number,"3"); gcry_mpi

Python – Have a variable be both an int and a str

╄→гoц情女王★ 提交于 2019-12-13 00:29:53
问题 Here is my code: def retest2(): print "Type in another chapter title! Or type \"Next\" to move on." primenumbers2() def primenumbers1(): print "--------------------------------------------------\nChapters in books are usually given the cardinal numbers 1, 2, 3, 4, 5, 6 and so on.\nBut I have decided to give my chapters prime numbers 2, 3, 5, 7, 11, 13 and so on because I like prime numbers.\n\nType in the chapter title of my book (a prime number) and I will tell you what cardinal number the

Largest prime factor of a number in Java

↘锁芯ラ 提交于 2019-12-12 18:51:17
问题 I am trying to find the Largest prime factor of a number while solving this problem here. I think that I am doing everything right, however one of the test case (#2) is failing and I can't think of any corner case where it might fail. Here's my code, please have a look and try to spot something. public class ProblemThree { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); for (int i = 0; i < T; i++) { System.out.println(largestPrime