Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Rabin Miller Primality test

Name: Anonymous 2015-05-06 19:42

Meh
// Find a witness for the Rabin-Miller test
// http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
// (variable names following article)

#include <stdio.h>
#include <math.h>

int gcd(int a, int b) {
int c;
while(a != 0) {
c = a;
a = b%a;
b = c;
}
return b;
}

int maxpowerof2(int n) {
int r = 0;
while(n % 2 == 0) {
n /= 2;
r++;
}
return r;
}

int main(void) {
int r, n, a, i, s, d;
printf("Give n: ");
fflush(stdout);
scanf("%d", &n);
s = maxpowerof2(n - 1);
d = (n - 1)/pow(2, s);
printf("n - 1 = 2^s * d\n"
"%d = 2^(%d) * %d\n", n - 1, s, d);
for(a = 1; a < n; a++) {
if(gcd(a, n) == 1) {
if((int)pow(a, d) % n != 1) {
for(r = 0; r < s; r++)
if((int)pow(a, pow(2, r)*d) % n == n - 1) break;
if(r == s) {
printf("Found a witness: %d\n", a);
break;
}
}
}
}
return 0;
}

Name: Anonymous 2015-05-06 20:48

AKS is superior.

Name: Anonymous 2015-05-06 23:43

Did you write this code?

Name: Anonymous 2015-05-07 0:01

>>2
this is a code off, you have to post your code for AKS

Name: Anonymous 2015-05-07 3:02

>>4
def AKS(a):
prime = True
for i in range(2, a):
if a % i == 0:
prime = False
return prime

Name: Anonymous 2015-05-07 3:45

>>5
that's not aks, and it's really inefficient too. you gotta do this at least:

my @primes := 2, 3, 5, -> $p { ($p+2, $p+4 ... &is-prime)[*-1] } ... *;

Name: Anonymous 2015-05-07 5:34

>>5
You could just do range(3,sqrt(a),2) after checking if it's even.

Name: Anonymous 2015-05-07 16:06

//primefactors.c
#include "void.h"
//https://gist.github.com/FrozenVoid/87e6ad6212ac9ce496e0

STDSTART
if(argc!=2)quit("Syntax:",argv[0]," number");
u8 z=str2num(argv[1],z);
primefactors(z);
STDEND

Name: Anonymous 2015-05-07 17:37

>>8
wow, void.h even includes solutions to the first three project euler problems for maximum bloat

Name: Anonymous 2015-05-07 17:51

>>9
Technically only the primefactor function is compiled in, others are macros and are only used when explicitly called in the code.
Unused macros are zero-cost abstraction.

Name: Anonymous 2015-05-07 18:13

>>9
converted it to macro now, so no bloat.

Name: Anonymous 2015-05-07 18:23

>>10
forcing the preprocessor to deal with hundreds of lines of useless shit isn't zero cost.

Name: Anonymous 2015-05-07 18:32

>>12
HUNDREDS OF LINES OF PURE, UNADULTERATED CPU-TAXING CODE

DO YOU KNOW HOW MANY TIME IT WILL TAKE?!?

WHOLE NANOSECONDS COULD BE WASTED

Name: Anonymous 2015-05-07 18:49

>>13
you're right, why would anyone in their right mind precompile headers.
don't they know that they are zero cost abstractions.

Name: Anonymous 2015-05-07 18:49

>>12,13
eh, this isn't C++ where ADL and template hell make compiles slow.
Its the C text preprocessor. Its built for speed.

Name: Anonymous 2015-05-07 18:53

There could be a slight slowdown due nested proxy() macro if you use some complex nested macros like eval too much(all _VFUNC macros can be nested about 3^5 layers ).

Name: Anonymous 2015-05-08 2:03

>>15
Running a token substituting preprocessor before the parser is one of the slowest and stupidest things a compiler can do. C++ compiles are slow because the preprocessor is a shit mechanism for sharing template definitions among compilation units.

Name: Anonymous 2015-05-08 4:57

My anus is built for speed. Give it all you got.

Name: Anonymous 2015-05-08 4:59

>>18
Prepare yourself. You will curse the day you asked me to unleash my SCALABLE HIGH-PERFORMANCE PENETRATION TESTING SOLUTIONS on you.

Name: Anonymous 2015-05-08 5:02

>>20
aw stfu plz

Name: Anonymous 2015-05-08 6:52

>>8
Its just brute-force division, there should be a faster method.

Name: Anonymous 2015-05-08 7:04

>>21
I agree. Lubricating first is more efficient than brute-force division of the anus.

Name: Anonymous 2015-05-08 7:26

>>3
Yes, except gcd which I shamelessly googled.

Name: Anonymous 2015-05-10 6:24

>>23
Have you read your SICP today?

Name: Anonymous 2015-05-10 8:43

>>24
Have you read my SICP today?

Name: Anonymous 2015-05-10 8:49

We've all read our SICP today.

Name: Anonymous 2015-05-10 11:53

Name: Anonymous 2015-05-10 15:19

Rabbi Miller Primatality Test

Name: Anonymous 2015-05-12 16:15

>>28
How old are you 12? Its either that or you're clearly retarded either way. I'd rather be 5ft 6 than be as retarded as you.

Name: Anonymous 2015-05-12 17:10

>>25
Hi, Sussman.

Name: Anonymous 2015-05-12 17:45

>>24
Why, am I on it?

Name: Anonymous 2015-05-12 22:58

>>30
You got me. It's just you, me, and the other guy.

Name: Anonymous 2015-05-12 23:18

while(a != 0)

Why do people do this shit?

Name: Anonymous 2015-05-12 23:28

>>33
Because it's not a bool. Readability, basically, the only concession C programmers make towards that...

Name: Anonymous 2015-05-13 14:33

>>33
fo'real.

Name: Anonymous 2015-05-14 6:25

>>34
Because it's not a bool.

It makes no difference (not if you're using _Bool, anyway, and not some other disgusting pre-C99 hack).

May people's motivation for this is the fact that

while (!a)

is really easy to mistake for

while (a)

if you've been staring at your display too long and are slowly going blind. The following are also popular with the Perl crowd, for the same reason:

#define until(a) while (!(a))
#define unless(a) if ((!a))

Name: Anonymous 2015-05-14 6:28

>>36
while (!a)
is really easy to mistake for
while (a)
I'm so annoyed that I'm not even going to click (Post truncated). If you are that fucking retarded, then don't even bother trying to program.

Name: Anonymous 2015-05-14 9:26

>>37
To be fair, >>36-chan's point was that the people who do that do that because they are going blind from constant masturbation to Chinese pornographic comics, not because they are that retarded.

Name: Anonymous 2015-05-14 9:35

>>38
go back to /g/!!!

Name: Anonymous 2015-05-14 23:45

>>38
When you say chinese to purposedly show your ignorance on anime you aren't making a good point.

Name: Anonymous 2015-05-15 8:37

>>38-40
YHBT

Name: Anonymous 2015-05-16 14:42

>>41
It's not anime, it's manga
YHBTIHBT

Don't change these.
Name: Email:
Entire Thread Thread List