Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #17686

Re: Primality by Wilson Theorem

From janus <emekamicro@gmail.com>
Newsgroups comp.lang.c
Subject Re: Primality by Wilson Theorem
Date 2012-02-20 11:12 -0800
Organization http://groups.google.com
Message-ID <12827221.664.1329765164046.JavaMail.geo-discussion-forums@yncd8> (permalink)
References <21835173.5344.1329724442804.JavaMail.geo-discussion-forums@vbtb2> <d215k7lhp2giliim2vrrpfbo6ooj7cjm8h@4ax.com>

Show all headers | View raw


> wrote:
> 
> ># include <stdio.h>
> >
> >
> >long long int  factorial( long long int num){
> >	long long int tempNum ;
> >	long long int acc = 1;
> >	for(tempNum = 1; tempNum < num + 1; ++tempNum){
> >		acc = acc * tempNum;
> 
> The first iteration contributes nothing to the result.  You could
> start with tempNum set to 2
> 
> >	}
> >	return acc;
> >	
> >}
> >
> > long long int  cosAndFactorial( long long int  num){
> >	long long int factValue = factorial(num - 1);
> >        long long int result = (factValue + (long long int)1 ) % num;
> 
> Why the cast?  You didn't put a cast in expression-2 of the for loop
> in factorial().  The constant would be promoted automatically without
> it.  If you are really concerned, why not change 1 to 1LL to start
> with, though any decent compiler should do that automatically.
> 
> >	return result;
> >	
> >}
> >
> >int main (){
> >	long long  int i;
> >	int pcount = 0;
> >	long long int value;
> >	for( i =  3; i < 1000000; i = i + 2){
> >
> >		value = cosAndFactorial(i);
> >
> >		if((int)value == 0){
> 
> Why the cast?  Is there some reason you only want to test the low
> order 32 bits?  Are you aware that every factorial above 33! has at
> least 32 low order zeros when expressed in binary.  This explains the
> result you see for 35.
> 
> >			++pcount;
> >			printf("Factorial : This number %llu is a Prime\n", i);
> >		}
> >		if(pcount == 10) {
> >			break;
> >		}
> >	}
> >	
> >return 0;
> >}	
> >
> >I am trying to code Wilsons Theorem, http://mathworld.wolfram.com/WilsonsTheorem.html?affilliate=1
> >The above returns;
> >Factorial : This number 3 is a Prime
> >Factorial : This number 5 is a Prime
> >Factorial : This number 7 is a Prime
> >Factorial : This number 11 is a Prime
> >Factorial : This number 13 is a Prime
> >Factorial : This number 17 is a Prime
> >Factorial : This number 19 is a Prime
> 
> 21! requires 66 bits so it does not fit into a 64 bit value.  How
> large is a long long on your system.  Hint: your code did not work for
> 23.
> 
> Even if long long were 128 bits, 34! would not fit.
> 
> 
> >Factorial : This number 35 is a Prime
> >
> >
> >Regards, \Janus
> 
> -- 
> Remove del for email

Thank you all. I have learnt a couple of things.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Primality by Wilson Theorem janus <emekamicro@gmail.com> - 2012-02-19 23:54 -0800
  Re: Primality by Wilson Theorem Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-20 00:39 -0800
    Re: Primality by Wilson Theorem Noob <root@127.0.0.1> - 2012-02-20 11:50 +0100
  Re: Primality by Wilson Theorem "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-02-20 01:15 -0800
  Re: Primality by Wilson Theorem pacman@kosh.dhis.org (Alan Curry) - 2012-02-20 09:31 +0000
    Re: Primality by Wilson Theorem gwowen <gwowen@gmail.com> - 2012-02-20 03:44 -0800
  Re: Primality by Wilson Theorem Barry Schwarz <schwarzb@dqel.com> - 2012-02-20 10:33 -0800
    Re: Primality by Wilson Theorem janus <emekamicro@gmail.com> - 2012-02-20 11:12 -0800
      Re: Primality by Wilson Theorem Paul <nospam@needed.com> - 2012-02-20 16:21 -0500
        Re: Primality by Wilson Theorem janus <emekamicro@gmail.com> - 2012-02-21 03:08 -0800
    Re: Primality by Wilson Theorem Ike Naar <ike@iceland.freeshell.org> - 2012-02-20 22:52 +0000
      Re: Primality by Wilson Theorem Ike Naar <ike@iceland.freeshell.org> - 2012-02-20 22:55 +0000
  Re: Primality by Wilson Theorem Robert Miles <milesrf@Usenet-News.net> - 2012-03-31 00:38 -0500
    Re: Primality by Wilson Theorem 88888 Dihedral <dihedral88888@googlemail.com> - 2012-04-03 12:06 -0700

csiph-web