C: How to get a 4096 bit prime number? [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
How to get a random, really big (f.e. 4096 bit) prime number in C?
Does anyone know a good Library for this?

Your best bet is libgmp.
It has a function that will scan for the next prime number (using Miller-Rabin) starting from some starting number.
void mpz_nextprime ( mpz_t rop, mpz_t op );
Set rop to the next prime greater than op.
This function uses a probabilistic algorithm to identify primes. For practical purposes it's adequate, the chance of a composite passing will be extremely small.
Is the function you want.
You just roll a random number with as many bits as you need and then fire mpz_nextprime. Runtime should be somewhere around O(log(op)) (probabilistic).
You will also need one of the random number generators.

Generally you generate a large random number, using a strong random number generator (e.g. on Windows use CryptGenRandom), then apply some checks to determine whether it is likely to be prime.
The only way to check that it really is prime is to try dividing by every number between 1 and (potential-prime / 2). If any of them divides equally with no remainder, it's not prime. Since that will take an infeasibly long time to compute (that's the whole point of using really big prime numbers), the tests used are far simpler and based on the probability that the number is unlikely to have easily guessable factors.
If you're implementing software that uses encryption, I strongly recommend that you use a NIST-certified cryptographic library or module to generate your keys and do the encryption.

Related

Calculate using Rational Index Binomial Theorem in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I have tried so many ways to calculate using this binomial theorem but I still couldn't find one:
The value of x and n is given for example b=0.5 and n=8
I know for the factorial we have to use loop but the numerator part is a little bit tricky.
Obviously I know how to code for (1+b)^n, but the question is still asking for the coding for binom theorem.
For example if the value of x is 0<x<1 and n is any positive integer, what will the value of (1+x)^n will be using the binomial theorem?
I understand that you know how to calculate the left side of the equation in programming.
I understand that you also know how to program the right side, apart from the problem that it is an infinite loop; but you want it to end at some point and have a result.
By the math theory ending early means a wrong result.
But in programming you will have problems with restricted precision of floating point math anyway. So you can take shortcuts to solve your problem.
In the comments you find recommendations how to do the calculation of each step efficiently. I will only focus on the end condition.
Write a loop calculating more and more precise steps.
End the loop when a freshly calculated (intermediate) result is the same as the previous one. With floating point representation having restricted precision that will sooner or later happen and the result will be within only one "minimal rounding" of the correct result.
Note:
In order to avoid the restricted precision getting in the way at the wrong place, I recommend to calculate the parts (as described in the recommendation in comments) in double and the intermediate results (those you compare for the loop condition) into a float variable.

How to predict Rand() function in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to make an oracle which predicts next random number in a sequence. I have an array of random generated numbers.
The rand() function is a Pseudo-Random Number Generator (PRNG). It is not a cryptographically secure source of entropy. If you know the seed, you can completely predict the sequence as it is deterministic, typically based on Linear Congruential Generator (LCG). Such generators have a finite period length, after which they repeat.
If you know the given sequence starts from the beginning, it would be trivial to brute-force the seed to find matching initial sequence. Otherwise there are statistical methods you could use to narrow down the potential seeds.
If you have actual random numbers, there's no way to predict them.
Software can be programmed to use actual random numbers that are acquired from monitoring random events like background radiation, atomic decay and electrical noise from various components. This is usually used only for critical applications like creating cryptographic keys, since the operation will block until enough "Random bits" have been collected.
Most software uses an algorithm that creates random-looking numbers based on a seed and past events like calls to the PRNG, elapsed time, etc. These are possible to predict with 100 accuracy if you know the algorithm used and all the events it uses for inputs, or have the ability to reset the seed to a known value.

Number of prime number between 1 and n [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have been surfing on the internet and found an interesting video in which is mentioned that you can find number of primes between 1 and any number n using Riemann hypothesis and Riemann zeta function. My math knowledge is not this high and I don't understand how, using zeta function, can one find number of primes.
I wanted to write a program that takes one number as input and outputs number of primes to that number, which is calculated using aforemention zeta function, but I have no idea where to start learning. Please know that I'm 17 years old and have always loved math and programming but this is something totaly new to me. Any help is apreciated.
There are some formulas, but the best we have so far is only asymptotic estimates.
It is shown that if we denote with π(n) the number of primes that do not exceed n then the fraction:
π(n) * ln(n) / n
can be arbitrarily close to 1.
This is the prime number theorem.

Finding abundant numbers from 1 to 10 million using a sum [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
My task is to implement algorithm in C of finding abundant numbers from 1 to 10 million. Therefore I don't really understand mathematics.
There is several ways how to do it, but efficient and fast (for that BIG input 10 mil) might be by summing - NOT dividing, NOT multiplying, NOT EVEN using remainder after the division. Just sum.
But I'm really confused WHAT to sum. Please guys help, appreciate every single answer.
Only I know is that there are 2476736 abundant numbers under 10
million, common computer hardware is not able to check it even in
hours, so I need more efficient algorithm and I know it's able to run
under a second.
you could try this by counting all the multiples of an abundant number upto 10 million
suppose 12 is the first abundant number you found then 24 would definetly be abundant hence you can count all the multiples of 12 upto the limit you wish then go for the next number.I don't know how fast or efficient it would be.

Looking for a tool that would tell me which integer-widths I need for a calculation in C to not overflow [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I have a lengthy calculation (polynomial of 4th degree with fixed decimals) that I have to carry out on a microcontroller (TI/LuminaryMicro lm3s9l97 [CortexM3] if somebody is interested).
When I use 32bit-Integers, some calculations flow over. When I use 64bit Integers the compiler emits an ungodly amount of code to simulate 64bit-multiplication on the 32bit-processor.
I am looking for a program into which I could input (just for example):
int a, b, c;
c = a * b; // Do the multiplication
c >>= 10; // Correct for fixed decimal point
c *= a*b;
where I could specify, that a and b would be in the range of [15000..30000] [40000..100000] respectively and it would tell me what sizes the integers need to not overflow (and/or underflow; I would possibly get a false positive there for the >> 10) in the specified domain, so that I could use 32bit-integers where possible.
Does something like this exists already or do I have to roll my own?
Thanks!
I think you have to roll your own. Implementing an extended sequence of muls and divs in fixed-point can be tricky. If fixed-point is applied without careful thought, overflow can happen quite easily. When implementing such a formula, I use a spreadsheet to experiment with the following:
Ordering of operations: muls require twice the number of bits on the left-hand side, i.e. multiplying two 22.10 numbers can yield a 44-bit result. Div operations reduce the number needed on the LHS. Strategically re-ordering the equation's evaluation, or even rewriting it (expanding, factoring, etc) can provide opportunities to improve precision.
Pre-computed scalars: along the same lines, pre-computing values may help. These scalars may not be need to be constant, since look-up tables may be used to store a collection of pre-computed values.
Loss of precision: is 10-bits of precision really needed at steps in the evaluation of the equation? Perhaps some steps need lower precision, leaving more bits on the LHS to avoid overflow.
Given these concerns (all of which are application-specific), optimal use of fixed-point math remains very much a manual exercise. There are good resources on the web. I've found this one useful on occasion.
Ada might be able to do that using range types.

Resources