Jump to content

Primes


Gargamel

Recommended Posts

A small step for a human:

img_163a2f3e0132529807513ac90b9824f24711

Spoiler

#include <stdio.h>

static inline int isPrime( int n ) {
	if( 0 == ( n % 2 ) )
		return 0;
	if( 0 == ( n % 3 ) )
		return 0;
	if( 0 == ( n % 5 ) )
		return 0;
	int l = n/2;
	for( int c = 5; c <= l; ++c ) {
		if( 0 == ( n % c ) ) {
			//printf( "%d is divisable by %d\n", n, c );
			return 0;
		}
	}
	return 1;
}

int main( void ) {
    // Edit these numbers to pick a range for calculation:
	for( int n = 830; n < 900; ++n )
		if( 1 == isPrime( n ) )
			printf( "%d is prime.\n", n );
	puts( "The end !\n" );
	fflush( stdout );
	return 0;
}

 

Don't laugh about its primitiveness :blush:, but to anybody with a c compiler (all linux users) this might be helpful and a play inside the game :-) Ping me if you like a command line version like "printprime lower upper".

 

 

Edited by Green Baron
Link to comment
Share on other sites

883

Pezzali_883.jpg

6 hours ago, Green Baron said:

[...]

  Reveal hidden contents


#include <stdio.h>

static inline int isPrime( int n ) {
	if( 0 == ( n % 2 ) )
		return 0;
	if( 0 == ( n % 3 ) )
		return 0;
	if( 0 == ( n % 5 ) )
		return 0;
	int l = n/2;
	for( int c = 5; c <= l; ++c ) {
		if( 0 == ( n % c ) ) {
			//printf( "%d is divisable by %d\n", n, c );
			return 0;
		}
	}
	return 1;
}

int main( void ) {
    // Edit these numbers to pick a range for calculation:
	for( int n = 830; n < 900; ++n )
		if( 1 == isPrime( n ) )
			printf( "%d is prime.\n", n );
	puts( "The end !\n" );
	fflush( stdout );
	return 0;
}

 

Don't laugh about its primitiveness :blush:, but to anybody with a c compiler (all linux users) this might be helpful and a play inside the game :-) Ping me if you like a command line version like "printprime lower upper".

 

 

This way is a bit faster and keeps track of gaps:

import math
#import matplotlib.pyplot as plt

def isprime(number):
    if number == 1:
        return False
    elif number < 4:
        return True
    elif number % 2 == 0:
        return False
    elif number < 9:
        return True
    elif number % 3 == 0:
        return False
    else:
        i = 5
        while i <= math.ceil(math.sqrt(number + 0.5)):
            if number % i == 0:
                return False
            if number % (i + 2) == 0:
                return False
            i += 6
        return True

prev_prime = 0
prime = 0
gap = 0
max_gap = 0
primes = []
gaps = []

for i in range(1, 10000):
    prev_prime = prime
    if isprime(i):
        prime = i
    gap = prime - prev_prime
    #primes.append(prime)
    #gaps.append(gap)
    if gap >= max_gap:
        max_gap = gap
        print("the primes:")
        print(prev_prime)
        print(prime)
        print("The gap")
        print(gap)

#plt.bar(primes, gaps, width=5)
#plt.show()
print(max_gap)

 

Link to comment
Share on other sites

On 12/18/2018 at 12:09 AM, qzgy said:

631!

Gap of... 12 I think. 

 

On 12/18/2018 at 6:47 PM, mabdi36 said:

673! Gap of 12 @Gargamel

I thought I warned you...  :huh:

3783289466256320478898042832094887452811366241564260711030689288561886940588046068690027219641097073851455820895101975073788224619213182637233976476211243402976039856958438443961386162515182765652796454163901930854267000117035896706603031609891821584289471477743241214582528792544026146683882033507780068309815664832001817356264972401024618769906980842018635588217705273149252035324629268922023435822415740038586023117148054448507607779407014989014665556876016341779467003092777506854851387736550050775543417770772589155258132920059467108066815287513562322535800113689788554674950277106110039999967309016391250121526069647070432631609722242128161056567286928290513876178684843814711697738835446983626882172329010122591736178865843369496728811605820828305279047545197942348909818202223658917042798916329181998681311811762067481182802705493565333806823338552940113516463945462591903169688308466200727342827799987774370433799941058287006650059685115543780857741662277910013586557439277994966285962028667946512075952673933994659057275659249429552275887741172539288080335062613667661775319721076585968747076736327671004964106568041668269695075458743434448603515090922562336594479717542686236053310821669807903972094970373323128215860285905784668084033362015299308760110495315879122021346314432618600376194822527472738824537250465427874140651520000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 is clearly divisible by 10.

As is  61249584099358401539774988285121649211647782880181065019552657036267338153088195303988201779967275642784589505913349592976251572958797164520286603082616258499126414850388770750032832244874744865500684599339365169094265281656246018624169125087086336929008659140773790287427038315506740711640971717627407262119806133914039569804387544893605360482632749642132398074143010093832414811273406748220437584361624445361171146706501836044960640727879585735220969146850637281930634576684379022439144569827759897323120413808197447743317836963898751450642251281351982277623696403714801809091137618510094637754741546381374172490209156669750628265287758243565040756752491082629092890931663069084118249960190350279925210044221389170848672643624902424798289485981643559009642358060100976306359010066013465973059932028926310180595315985960099791957394179039519432507444190747654625992620055591848528852607925564873303749001475451862943569149219508203963665660697011849205174996326078837279628237406181221912723812127044670946612175065696608648876366755523800502033220426264259724448110042998615347327090687044945724644868095726898042638404229137017574884525227292991943592508583104116919096883640157188742952660337139750108570879849335960456768856494175006057451288109527150100807278246132549650716938934121106772599000305859091685578549764454500874996178837114679306052077693402114826710945748516120895211107804543955416170463298478450007640457295281818515443548160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

==========================

The next prime number is 947.

Link to comment
Share on other sites

On 12/20/2018 at 2:51 PM, Mad Rocket Scientist said:

883

This way is a bit faster and keeps track of gaps:


import math
#import matplotlib.pyplot as plt

def isprime(number):
    if number == 1:
(...)
#plt.show()
print(max_gap)

 

Is this a challenge?

m = 10000

def isprime(n):
    return all(n % i for i in range(2, n))

primes = [i for i in range(2, m + 1) if isprime(i)]
gaps = [j - i for i, j in zip(primes, primes[1:])]
print(max(gaps))

 

Anyway, 947

947wls_opengraph-general_1200x630_01.jpg

Edited by Kerbart
Used "quote" instead of "code"
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...