prime-num | math library for JavaScript and Node.js | Math library
kandi X-RAY | prime-num Summary
kandi X-RAY | prime-num Summary
prime-num is an math library for JavaScript and Node.js. It help you to find all prime number.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of prime-num
prime-num Key Features
prime-num Examples and Code Snippets
Community Discussions
Trending Discussions on prime-num
QUESTION
Hash Functions are incredibly useful and versatile. In general, they are used to map a space to one much smaller space. Of course that means that two objects may hash to the same value (collision), but this is because you are reducing the space (pigeonhole principle). The efficiency of the function largely depends on the size of the hash space.
It comes as a surprise then that a lot of Java hashCode functions are using multiplication to produce the hash code of a new object as e.g. follows (creating-a-hashcode-method-java)
...ANSWER
Answered 2021-Jun-04 at 11:47The answer to this is a mixture of different factors:
- On modern architecture, the time taken to perform a multiplication versus a shift may not end up being measurable overall within a given pipeline of instructions-- it has more to do with the availability of the relevant execution unit on the CPU than the "raw" time taken;
- In practice when integrating with standard collections libraries in day-to-day programming, it's often more important that a hash function is correct, "good enough" and easy to automate in an IDE than for it to be as perfect as possible;
- The collections libraries generally add secondary hash functions and potentially other techniques behind the scenes to overcome some of the weaknesses of what would otherwise be a poor hash function;
- With resizable collections, an effective hash function has the goal of dispersing its hashes across the available range for arbitrary sizes of hash tables (though as I say, it will get help from the built-in secondary function): multiplying by a "magic" constant is often a cheap way to achieve this (or, even if multiplication turned out to be a bit more expensive than a shift: still cheap enough, given the benefit); addition rather than XOR may help to allow this 'avalanche' effect slightly. (In most practical cases, you will probably find that they work equally well.)
- You can generally assume that the JIT compiler "knows" about equivalents such as shifting 5 places and subtracting 1 rather than multiplying by 31. Just because you write "*31" in the source code doesn't mean that it will literally be compiled to a multiplication instruction. (In practice, it might be, though, because despite what you think, the multiply instruction may well be "faster" on average on the architecture in question... It's usually better to make your code stick to the required logic and let the JIT compiler handle the low level optimisations in a case such as this.)
QUESTION
https://www.quora.com/Why-should-the-size-of-a-hash-table-be-a-prime-number?share=1
I see that people mention that the number of buckets of a hash table is better to be prime numbers.
Is it always the case? When the hash values are already evenly distributed, there is no need to use prime numbers then?
https://github.com/rui314/chibicc/blob/main/hashmap.c
For example, the above hash table code does not use prime numbers as the number of buckets.
https://github.com/rui314/chibicc/blob/main/hashmap.c#L37
But the hash values are generated from strings using fnv_hash
.
https://github.com/rui314/chibicc/blob/main/hashmap.c#L17
So there is a reason why it makes sense to use bucket sizes that are not necessarily prime numbers?
...ANSWER
Answered 2021-May-20 at 16:48The answer is "usually you don't need a table whose size is a prime number, but there are some implementation reasons why you might want to do this."
Fundamentally, hash tables work best when hash codes are spread out as close to uniformly at random as possible. That prevents items from clustering in any one location within the table. At some level, provided that you have a good enough hash function to make this happen, the size of the table doesn't matter.
So why do folks say to pick tables whose size is a prime? There are two main reasons for this, and they're due to specific cases that don't arise in all hash tables.
One reason why you sometimes see prime-sized tables is due to a specific way of building hash functions. You can build reasonable hash functions by picking functions of the form h(x) = (ax + b) mod p, where a is a number in {1, 2, ..., p-1} and b is a number in the {0, 1, 2, ..., p-1}, assuming that p is a prime. If p isn't prime, hash functions of this form don't spread items out uniformly. As a result, if you're using a hash function like this one, then it makes sense to pick a table whose size is a prime number.
The second reason you see advice about prime-sized tables is if you're using an open-addressing strategy like quadratic probing or double hashing. These hashing strategies work by hashing items to some initial location k. If that slot is full, we look at slot (k + r) mod T, where T is the table size and r is some offset. If that slot is full, we then check (k + 2r) mod T, then (k + 3r) mod T, etc. If the table size is a prime number and r isn't zero, this has the nice, desirable property that these indices will cycle through all the different positions in the table without ever repeating, ensuring that items are nicely distributed over the table. With non-prime table sizes, it's possible that this strategy gets stuck cycling through a small number of slots, which gives less flexibility in positions and can cause insertions to fail well before the table fills up.
So assuming you aren't using double hashing or quadratic probing, and assuming you have a strong enough hash function, feel free to size your table however you'd like.
QUESTION
I try to create a shorter algorithms to guess if a number is a prime-number or not
So there is the algorithms that I want to short
...ANSWER
Answered 2021-Feb-02 at 11:55You didn't manage to translate the first function correctly, no.
The function
is_premier()
returns a single boolean value, whileis_prime_v3()
returns a generator of strings.The
is_prime_v3()
generator produces multiple"Prime"
values for non-prime numbers, e.g. for8
, the test8 % 3 == 0
is false, so"Prime"
is generated:
QUESTION
I have two versions of a function: each one does part of what I want but I cannot implement everything in a single function. I am not seeing where the disconnect is.
I tried to deduce this in a different post for the formatting as a simple example while also referencing this. When plugging in to my function though, I cannot figure it out.
The first version has the intended format but returns all numbers, not only the primes.
...ANSWER
Answered 2021-Feb-02 at 03:17Step one: Move the logic for prime testing to a standalone function.
QUESTION
I'm new to python and im trying to make a small program that checks if the number is prime/non prime and odd/even at the same time, the fact that i wrote for i in range(2,num):
and inside that condition there is if (num % i) == 0:
to check if its prime or not, but it is only checking the i=2
at the first time and after i put the first number and it increments, what im asking is that why it isnt checking for i=3..4..5..
to see if its prime or not
Because when I test it and put number 21
the first time, it says that its prime and if i put 21
the second time it tests on i=3
and says its not prime.
And another problem, i wrote while (num != 0):
so the program should stop after entering a value 0
but its still running till i enter it multiple times to stop.
ANSWER
Answered 2020-Nov-25 at 09:48The break is important!
In your code the for loop was looping, but was never breaking until it reached your original number. You need to break out of the for loop once a prime number is found. If the loop completes without breaking, the else statement will be run. The user input should then be outside of the loop and after the else statement.
As a side note, in Python, brackets are not required on if and while statements, although I did not edit those.
QUESTION
I have solved Project Euler problem 12 and I tried to optimize my solution.
The part I am focusing on is the part of finding the number of divisors.
The first algorithm I created I thought was going to be slower than the second but it wasn't and I don't understand why?
First(regular count goes until n**0.5):
...ANSWER
Answered 2020-Nov-01 at 05:44In the first approach, you testing divisibility with each number from 1 to sqrt(x)
, so the complexity of testing a single number is sqrt(x)
. According to this formula, the sum of first n
roots can be approximated to n*sqrt(n)
.
Time complexity of method 1: O(N*sqrt(N))
(N is the total count of numbers being tested).
In the second approach, there are 2 cases:
- If a number isn't prime, all primes upto
n
are tested. Complexity -O(n/6) = O(n)
- If a number is prime, we can approximate the complexity to be
O(log(n))
(there might be a more accurate calculation of the complexity for this case, I'm making an approximation since this wouldn't matter in the proof)
For the prime numbers, using the fact that we test them with (n/6)
primes, the complexity would become 5/6 + 7/6 + 11/6 + 13/6 + 17/6 ..... (last prime before n)/6
. This can be reduced to (sum of all prime numbers till n)/6
for the time being. Now, the sum of all prime numbers upto N
can be approximated as N^2/(2*logN)
. Thus the complexity for this step becomes N^2/(6*(2*logN)) = N^2/(12*lognN)
.
Time complexity of method 2: O(N^2/(12*lognN))
(N is the total count of numbers being tested).
(if you want, you can make more accurate bounds for the time complexities of each step. I have made a few approximations since it helps in proving the point without making any overoptimistic assumption).
QUESTION
Good morning, am getting stuck with this kata, anyone can explain me ? In this kata i have to return the sum of elements occupying prime-numbered indices. I started my code like that, but didn't know what to do next. THANK YOU in advance.
...ANSWER
Answered 2020-Aug-05 at 09:57Please use
;
after breaking condition which isi < arr.length
herefor (let i = 2; i < arr.length, i++) {}
You can loop over arr the way you are doing here and validate if index at which you are looping is prime itself.
If 2nd step is met you can add the value into summation var
sum
return
sum
.
Please follow this thread to find know more about calculating if value is prime. Number prime test in JavaScript
QUESTION
I am new to constexpr, however, I believe that problem I am practicing is a good fit compile-time calculations. This is not for homework or anything, just practicing "Modern" C++.
Here is what I have so far: Header (prime_app_lib.hpp):
...ANSWER
Answered 2020-Jul-14 at 20:47The issue is all your members need to be initialized before entering the constructor body. So you could do:
QUESTION
I'm deciding on a language to use for back-end use. I've looked at Go, Rust, C++, and I thought I'd look at Crystal because it did reasonably well in some benchmarks. Speed is not the ultimate requirement, however speed is important. The syntax is equally important, and I'm not averse to the Crystal syntax. The simple benchmark that I wrote is only a small part of the evaluation and it's also partly familiarisation. I'm using Windows, so I'm using Crystal 0.35.1 on Win10 2004-19041.329 with WSL2 and Ubuntu 20.04 LTS. I don't know if WSL2 has any impact on performance. The benchmark is primarily using integer arithmetic. Go, Rust, and C++ have almost equal performance to each other (on Win10). I've translated that code to Crystal, and it runs a fair bit slower than those three. Out of simple curiosity I also ran the code on Dart (on Win10), and it ran (very surprisingly) almost twice as fast as those three. I do understand that a simple benchmark does not say a lot. I notice from a recent post that Crystal is more efficient with floats than integers, however this was and is aimed at integers.
This is my first Crystal program, so I thought I should ask - is there any simple improvements I can make to the code for performance? I don't want to improve the algorithm other than to correct errors, because all are using this algorithm.
The code is as follows:
...ANSWER
Answered 2020-Jun-26 at 10:21You need to compile with --release
flag. By default, the Crystal compiler is focused on compilation speed, so you get a compiled program fast. This is particularly important during development. If you want a program that runs fast, you need to pass the --release
flag which tells the compiler to take time for optimizations (that's handled by LLVM btw.).
You might also be able to shave off some time by using wrapping operators like &+
in location where it's guaranteed that the result can'd overflow. That skips some overflow checks.
A few other remarks:
Instead of 0.to_i64
, you can just use a Int64 literal: 0_i64
.
QUESTION
We are doing the following programming exercise: Primes in numbers.
The task is to calculate the prime factor decomposition of a positive number.
First we have written:
...ANSWER
Answered 2020-Apr-11 at 10:37Use the following fact:
If n
is not prime, then it has a divisor d
such that d*d <= n
.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install prime-num
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page