primes

Why setting HashTable's length to a Prime Number is a good practice?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 18:43:57
I was going through Eric Lippert's latest Blog post for Guidelines and rules for GetHashCode when i hit this para: We could be even more clever here; just as a List resizes itself when it gets full, the bucket set could resize itself as well, to ensure that the average bucket length stays low. Also, for technical reasons it is often a good idea to make the bucket set length a prime number, rather than 100. There are plenty of improvements we could make to this hash table. But this quick sketch of a naive implementation of a hash table will do for now. I want to keep it simple. So looks like i

Why are primes important in cryptography?

狂风中的少年 提交于 2019-11-28 14:57:07
One thing that always strikes me as a non-cryptographer: Why is it so important to use Prime numbers? What makes them so special in cryptography? Does anyone have a simple short explanation? (I am aware that there are many primers and that Applied Cryptography is the Bible, but as said: I am not looking to implement my own cryptographic algorithm, and the stuff that I found just made my brain explode - no 10 pages of math formulas please :)) Thanks for all the answers. I've accepted the one that made the actual concept most clear to me. Most basic and general explanation: cryptography is all

Algorithm to find Lucky Numbers

余生颓废 提交于 2019-11-28 14:41:28
问题 I came across this question.A number is called lucky if the sum of its digits, as well as the sum of the squares of its digits is a prime number. How many numbers between A and B are lucky? 1 <= A <= B <= 10 18 . I tried this. First I generated all possible primes between 1 and the number that could be resulted by summing the squares (81 *18 = 1458). I read in A and B find out maximum number that could be generated by summing the digits If B is a 2 digit number ( the max number is 18

Racket Programming. Where am I going wrong?

扶醉桌前 提交于 2019-11-28 14:29:16
The question i'm trying to answer: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? Where am I going wrong? my prime? test seems to be the issue, but it works fine on relatively small numbers. However the prime? test gives a wrong answer with larger numbers. Is there an easier way to go about this? (define b 3) (define z 0) (define divides? (lambda (a b) (= (remainder a b) 0))) (define (prime? n) (cond ((or (= n 1) (= n 0)) false) ((even? n) false) ((= n 2) true) ((= n b) true) ((divides? n b) false) (else (and (set! b (+ b 1))

Prime Number Determination Javascript

六月ゝ 毕业季﹏ 提交于 2019-11-28 14:11:22
I am creating a external javascript file. This is for homework. What I am supposed to do is determine if the number that the user enters in is a prime number or not, and displays a message if it is a prime number or not. I have my code written, compiles and everything. But I am cannot seem to figure out with, no matter what number i enter in, the display message always says that that number is a prime number. Can anyone help? Here is my code: var UI; var TV; var HITS; UI = window.prompt("Enter a whole number to test as a prime number: \n", "0"); TV = parseInt(UI, 10); var DD = TV; //still

How do i reduce the space complexity in Sieve of Eratosthenes to generate prime between a and b?

こ雲淡風輕ζ 提交于 2019-11-28 11:47:54
After getting through some of the SO posts , i found Sieve of Eratosthenes is the best & fastest way of generating prime numbers. I want to generate the prime numbers between two numbers, say a and b . AFAIK, in Sieve's method, the space complexity is O( b ). PS: I wrote Big-O and not Theta, because i don't know whether the space requirement can be reduced. Can we reduce the space complexity in Sieve of Eratosthenes ? If you have enough space to store all the primes up to sqrt(b) then you can sieve for the primes in the range a to b using additional space O(b-a). In Python this might look like

MIPS Assembly code to find all the prime numbers below an inputted number

橙三吉。 提交于 2019-11-28 11:45:47
问题 Below I have posted my MIPS code. The java code I am basing it off on is... Java Code... for (int i=2;i<n;i++){ p = 0; for (int j=2;j<i;j++){ if (i % j == 0) p = 1; } if (p = 0) System.out.println(i); } I have added the line "beq $t3, 1, L4" to skip to L4 when p is set to 1 to save time. But when I added this line of code, the program outputs nothing. Before I added this line, it would print all the integers from 2~n. MIPS Code... # The number is read through the keyboard .text .globl main

Printing out Prime Numbers from 2 to 1000

99封情书 提交于 2019-11-28 11:23:02
问题 I am writing a code that write all the prime numbers from 2 to 1000 in a file, named primes.txt. For some reason I am not able to figure out the correct way to do this problem. import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; public class Problem6 { /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { PrintWriter prw = new PrintWriter("primes.txt"); for (int i = 2; i <= 1000; i++){ if

Merge of lazy streams (using generators) in Python

坚强是说给别人听的谎言 提交于 2019-11-28 11:18:50
问题 I'm playing with functional capacities of Python 3 and I tried to implement classical algorithm for calculating Hamming numbers. That's the numbers which have as prime factors only 2, 3 or 5. First Hamming numbers are 2, 3, 4, 5, 6, 8, 10, 12, 15, 16, 18, 20 and so on. My implementation is the following: def scale(s, m): return (x*m for x in s) def merge(s1, s2): it1, it2 = iter(s1), iter(s2) x1, x2 = next(it1), next(it2) if x1 < x2: x = x1 it = iter(merge(it1, s2)) elif x1 > x2: x = x2 it =

Prime number generation algorithm

时光毁灭记忆、已成空白 提交于 2019-11-28 09:53:54
问题 Please look at the following and see if you could advise. cout << "2" << endl; cout << "3" << endl; ofstream of("Primes.txt"); unsigned long prime = 0; unsigned long i = 1; for (i = 1; i < 100000; i++) { prime = ((i*2)+(i+1) + (i % 2)); of << prime << endl; } of.close(); return 0; The partially completed formula for calculating the nth prime The nth prime is spat out but so is all of its prime factors How to sieve through the list and find only primes. 5 7 11 13 17 19 23 25 29 31 35 37 41 43