primes

A number as it's prime number parts

…衆ロ難τιáo~ 提交于 2019-11-29 11:04:30
I have to print the number of ways you can represent a given number as it's prime number parts. Let me clarify: Let's say I have been given this number 7. Now, first of all, I have to find all the prime numbers that are less than 7, which are 2, 3 and 5. Now, in how many ways can I summarize those numbers (I can use one number as many times I want) so that the result equals 7? For example, number 7 has five ways: 2 + 2 + 3 2 + 3 + 2 2 + 5 3 + 2 + 2 5 + 2 I'm totally lost with this task. First I figured I'd make an array of usable elements like so: { 2, 2, 2, 3, 3, 5 } (7/2 = 3, so 2 must

efficient ways of finding the largest prime factor of a number

混江龙づ霸主 提交于 2019-11-29 09:40:03
问题 I'm doing this problem on a site that I found (project Euler), and there is a question that involves finding the largest prime factor of a number. My solution fails at really large numbers so I was wondering how this code could be streamlined? """ Find the largest prime of a number """ def get_factors(number): factors = [] for integer in range(1, number + 1): if number%integer == 0: factors.append(integer) return factors def test_prime(number): prime = True for i in range(1, number + 1): if i

How to pick prime numbers to calculate the hash code?

我们两清 提交于 2019-11-29 09:38:16
问题 This question follows on the answer given by Jon Skeet on the question: "What is the best algorithm for an overridden System.Object.GetHashCode?". To calculate the hash code the following algorithm is used: public override int GetHashCode() { unchecked // Overflow is fine, just wrap { int hash = 17; // Suitable nullity checks etc, of course :) hash = hash * 23 + field1.GetHashCode(); hash = hash * 23 + field2.GetHashCode(); hash = hash * 23 + field3.GetHashCode(); return hash; } } I don't

Finding the list of prime numbers in shortest time

心已入冬 提交于 2019-11-29 09:34:31
问题 I read lot many algorithms to find prime numbers and the conclusion is that a number is a prime number if it is not divisible by any of its preceding prime numbers. I am not able to find a more precise definition. Based on this I have written a code and it performs satisfactory till the max number I pass is 1000000. But I believe there are much faster algorithms to find all primes lesser than a given number. Following is my code, can I have a better version of the same? public static void

How to check if the value in a text box is prime or not with jQuery

馋奶兔 提交于 2019-11-29 08:53:10
I am trying to determine whether the value in a text box is prime or not using jQuery. Here is what I've tried so far, but it's not working: $("#textbx").keyup(function(){ if ($("#textbx").val().length > 0) { $("#btn").removeAttr('disabled'); } }); $("#textbx").blur(function(){ if ($("#textbx").val().length ==0) { $("#btn").attr('disabled','disabled'); } }); $("#textbx").keypress(function (e) { if (e.which!=8 && (e.which < 48 || e.which > 57)) { $("#msg").html("plz press nimber only").show().fadeOut("slow"); return false; } }); $("#btn").click(function(){ var num =parseInt($("#textbx").val());

Implementing the Page Segmented Sieve of Eratosthenes in Javascript

牧云@^-^@ 提交于 2019-11-29 08:43:39
I recently read about a faster implementation of Segmented Sieve of Eratosthenes for really big numbers. Following is an implementation of the same: function sieve(low, high) { var primeArray = [], ll = Math.sqrt(low), output = []; for (var i = 0; i < high; i++) { primeArray[i] = true; } for (var i = 2; i <= ll; i++) { if (primeArray[i]) { for (var j = i * i; j < high; j += i) { primeArray[j] = false; } } } for (var i = 2; i < ll; i++) { if(primeArray[i]) { var segmentStart = Math.floor(low/i) * i; for(var j = segmentStart; j <= high; j+=i) { primeArray[j] = false; } } } for(var i = low; i <=

Prolog Program To Check If A Number Is Prime

三世轮回 提交于 2019-11-29 07:42:24
I wrote the following program based on the logic that a prime number is only divisible by 1 and itself. So I just go through the process of dividing it to all numbers that are greater than one and less than itself, but I seem to have a problem since I get all entered numbers as true. Here's my code... divisible(X,Y) :- Y < X, X mod Y is 0, Y1 is Y+1, divisible(X,Y1). isprime(X) :- integer(X), X > 1, \+ divisible(X,2). Thanks in advance :) I'm a beginner in Prolog but managed to fix your problem. divisible(X,Y) :- 0 is X mod Y, !. divisible(X,Y) :- X > Y+1, divisible(X, Y+1). isPrime(2) :- true

Random prime number

自古美人都是妖i 提交于 2019-11-29 07:25:51
问题 How do I quickly generate a random prime number, that is for sure 1024 bit long? 回答1: Generate 1024 random bits. Use a random source that is strong enough for your intended purpose. Set the highest and lowest bits to 1. This makes sure there are no leading zeros (the prime candidate is big enough) and it is not an even number (definitely not prime). Test for primality. If it's not a prime, go back to 1. Alternatively, use a library function that generates primes for you. 回答2: Use a library

How can I convert an absolutely massive number to a string in a reasonable amount of time?

混江龙づ霸主 提交于 2019-11-29 05:21:55
问题 This is quite an odd problem I know, but I'm trying to get a copy of the current largest prime number in a file. Getting the number in integer form is fairly easy. I just run this. prime = 2**74207281 - 1 It takes about half a second and it works just fine. Operations are fairly quick as well. Dividing it by 10 (without decimals) to shift the digits is quick. However, str(prime) is taking a very long time. I reimplemented str like this, and found it was processing about a hundred or so digits

Factor a large number efficiently with gmp

陌路散爱 提交于 2019-11-29 04:36:58
I need to get all the prime factors of large numbers that can easily get to 1k bits. The numbers are practically random so it shouldn't be hard. How do I do it efficiently? I use C++ with GMP library. EDIT: I guess you all misunderstood me. What I mean by prime a number is to get all prime factors of the number. Sorry for my english, in my language prime and factor are the same :) clarification (from OP's other post): What I need is a way to efficiently factor(find prime factors of a number) large numbers(may get to 2048 bits) using C++ and GMP(Gnu Multiple Precession lib) or less preferably