Fuck your fizzbuzz meme. Write me a code that will print out prime numbers for a given range

Fuck your fizzbuzz meme. Write me a code that will print out prime numbers for a given range.

Attached: haxxor pepe2.jpg (720x428, 23K)

Other urls found in this thread:

lmgtfy.com/?q=collection.immutable.IndexedSeq
rosettacode.org/wiki/Sieve_of_Eratosthenes
stackoverflow.com/questions/18833759/python-prime-number-checker
algolist.net/Algorithms/Number_theoretic/Sieve_of_Eratosthenes
twitter.com/NSFWRedditVideo

>do my middle school homework
no.

Attached: 1521884470573.jpg (2880x2131, 1.2M)

It's not my homework. I just though it would be fun instead of fizzbuzzing all the time.
Here's my entry, though it's probably shit
#include
#include

int main() {
int max;
float result;
bool prime;
printf("Limit: ");
scanf("%d",&max);

for(int a=1; a

thx bro no homework tonight

Attached: 1523996491542.gif (540x304, 47K)

You're welcome, have fun playing Fortnite.

Attached: thumsup_hasselhoff.jpg (400x597, 35K)

>bool prime;
>if(prime == 1)
please be trolling
I haven't even looked at the algo yet

Attached: 22572e3b-46bf-4049-a366-c9622c25a2f7.jpg (400x429, 64K)

I'm a brainlet with no programming experience trying to get into C. I originally used int for prime since bool is not standard C, turns out there's a library for that.

>I haven't even looked at the algo yet
Any constructive critisism is welcome.

public PrimeTime{
public static void main(String [] args){
int maxNo;
Scanner sc = new Scanner();
System.out.println("enter max number");
maxNo = sc.nextInt();

for (int i = 0;i

Attached: coolFuck.jpg (340x372, 36K)

>no programming experience
>trying to get into C
uhhh
>constructive critisism is welcome
Get a book on C, a physical one if possible.

>i % 2 == 1
>if true is true
>no formatting
>bad algorithm that doesn't do what it should do
>java
Hello, pajeet

package main

import (
"fmt"
)

func main () {
fmt.Printf("Limit: ")
max := 0
fmt.Scanln(&max)
for i := range make([] int, max + 1) {
if isprime(i + 1) {
fmt.Println(i + 1)
}
}
}

func isprime(n int) bool {
if n

if (number) == prime

print

Some of the fucking replies in here. Jow Forums...

require 'prime'

puts("Limit: ")
max = gets.to_i

puts Prime.take(max)

Attached: ruby smug.png (200x200, 43K)

thank you mod operator, truly the hero when it comes to interview questions
needed one case hardcoded but I think it works assuming positive numbers and start

;(function (window) {

window.getPrimes = getPrimes;

function getPrimes(x) {
if (x < 2) {
return [];
}

var primes = [2];

for (var i = 3; i

Here we learned that Jow Forums doesn't know how to program.

Here's your PrimeBuzz
def isPrime(n: Int) = (2 to math.sqrt(n).toInt) forall {n%_ != 0}

@ (20 to 80) filter isPrime
res1: collection.immutable.IndexedSeq[Int] = Vector(23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79)

I've never seen this syntax before, what language is it?

discard even numbers (rest of dividing by 2 is 0).

compare if the rest of dividing it by all the odd numbers from 3 to n/2 is 0, if one is different than 0 discard.

There you go, it works flawlessly for any number of primes.

OK fuck stick, show us yours.

#include

int isprime(int n) {
if (n

constexpr bool isPrime(int possiblePrime){
if(possiblePrime < 2)
return false;
if(possiblePrime < 4)
return true;
if(possiblePrime == 5)
return true;
if(possiblePrime % 2 == 0)
return false;
if(possiblePrime % 3 == 0)
return false;
if(possiblePrime % 5 == 0)
return false;

const int next[8]={2, 6, 4, 2, 4, 2, 4, 6};
int index = 1;
for(int i = 7; i * i

>float result
I mean, we are talking about integers and such...

You can usually figure it out by searching for relatively unique-looking strings in the code, for example:
lmgtfy.com/?q=collection.immutable.IndexedSeq
However, it's a bit more difficult to figure out the environment, so I'll just say it directly: Ammonite.

>made me use Google
fuck you

you'd want to construct a code that basically acts like the Sieve of Eratosthenes up to n

but man do i wish i knew how to program. Thats basically how i'd answer during interview

Attached: 1511232630578.jpg (640x960, 90K)

>not opening untrusted links in separate browser context
shiggy

#include
using namespace std;

int main() {
int N;
int num = 0;
cin >> N;

for (int i = 2;i

let me try my for loop again:
int checker;
for (int i = 0; int i < maxNo; ++i){
++checker;
if (maxNo%checker=0){
System.out.println(i);
}
}

i'm so glad this site doesn't have user accounts

also
>not being aware of lmgtfy
that's like smugposting 101

motherfucker

wait maybe it should be if it doesn't equal 0... ah well i'm going to sleep

npm install get-primes

Attached: 1520983903738.png (1006x930, 81K)

how
eggsplain pls
am brainlet

If you dont understand my code you should switch fields

>missing an obvious improvement

constexpr bool isPrime(int possiblePrime){
if(possiblePrime < 2)
return false;
if(possiblePrime < 4)
return true;
if(possiblePrime % 2 == 0)
return false;
if(possiblePrime % 3 == 0)
return false;
if(possiblePrime < 25)
return true;
if(possiblePrime % 5 == 0)
return false;

const int next[8]={2, 6, 4, 2, 4, 2, 4, 6};
int index = 1;
for(int i = 7; i * i

Attached: no_bully_pls.png (657x527, 31K)

oh ok
so you check if it has exactly two factors rite
sorry m8 im very retarded today

learn how to properly space and indent your code, jesus.

also
>}[/code
well done retard

Here:
(p:inv@[ (

>for (int j = 1;j

public static void main(String[] args){
public boolean printPrimes(int max) {
for (int i=0, i

I fucked up I put a function inside a function kek, ignore the public static void main.

Also I'm retarded, why is the function a boolean, I'm just printing the numbers, it should be void.

(defun eratosthenes-sieve-adapted (a n)
(let ((number-arr (make-array (- n 1)))
(boolean-arr (make-array (- n 1) :initial-element t))
(prime-list (list)))
(loop for i from 2 to n
do (setf (aref number-arr (- i 2)) i))
(loop for i from 2 to (truncate (sqrt n))
do (if (aref boolean-arr (- i 2))
(let ((j (expt i 2)))
(loop while (= (aref number-arr k) a))
(push (aref number-arr k) prime-list)))
(reverse prime-list)))

[CODE]
IntStream.range(1, 100)
.filter(n -> !new String(new char[n]).matches(".?|(..+?)\\1+"))
.forEach(System.out::println)
[/CODE]

>Lisp
based

I looked it up for you, good luck on your final exams.
/* C++ Program - Print Prime Numbers */

#include
#include
void main()
{
clrscr();
int start, end, i, j, count=0;
// to print all the prime number between any range
// enter the two number (starting and ending)
coutstart;
coutend;
cout

for i in xrange(a, b):
cont = 1
while cont

When you apply what and pointed out this is not actually a bad code considering you're just started learning programming.
Also check if max is greater than zero, your program will misbehave with negative numbers.

primes = 2:[x | x

>eratosthenes sieve
This is fucking basic. Stoo making Jow Forums solve your project euler problems.

weirdly enough there's been only one eratosthenes sieve so far

Oops, Fixed.

IntStream.range(1, 100)
.filter(n -> !new String(new char[n]).matches(".?|(..+?)\\1+"))
.forEach(System.out::println)

/**
* Prints all prime numbers up to a given integer.
* @author Pajeet
* @version 1.0
* @since 1.0
*/
public class Sieve {
/**
* Computes the square of an integer.
* @param x Given integer
* @return {@code x}2
*/
private static int square(final int x) {
return x * x;
}

/**
* Program entry point.
* @param args Command-line arguments.
*/
public static void main(String[] args) {
if (args.length == 1) {
try {
final int max = Integer.parseInt(args[0]);
boolean[] sieve = new boolean[max + 1];
for (int i = 2; square(i)

Attached: Java.png (558x1023, 51K)

im on my phone and dont feel like writing code
i would have some sort of data structure, a linked list would work, store only the number 2 at the start
have an outer for loop while i

Attached: 1516057481024.png (1280x720, 472K)

based poo

That would work. You only need to check up to the sqrt of a number. Applying that optimization you basically get

i cant actually understand the code in the other post you quoted, but i understand what you mean. just have to add a break in the inner loop for when the current list number is greater than sqrt (i)
thanks user!

Attached: 1511857892795.gif (300x300, 93K)

you're doing rosettacode.org/wiki/Sieve_of_Eratosthenes IIUC

You're wrong. A sieve is an array of booleans that works with a fixed size and going ahead and prematurely marking stuff composite. The method he described is a list of primes that works by going back and checking if previous primes divide it.
The reason both method works is the same, but the algorithm is different.

This is possible in c++ with operator overloading.

val primes: Stream[Int] = 2 #:: Stream.from(3) .filter
{ n => primes .takeWhile{_

>You're wrong.
well, uh, ur mums wrong /s
Seriously, have folks forgotten what IIUC means?
Kids these days, no manners.

Yeah, you're right, good catch. Apparently it's a common mix-up
>Primes can also be produced by iteratively sieving out the composites through divisibility testing by sequential primes, one prime at a time. It is not the sieve of Eratosthenes but is often confused with it, even though the sieve of Eratosthenes directly generates the composites instead of testing for them

>so many people iterating over all numbers in range
>why not i+=2 instead of i++ starting in an odd number
>nobody is using a precomputed table of the first primes

main(int c, char** v){
for(int i=atoi(v[1]),t;i

Attached: autism.jpg (1000x1000, 297K)

>using floats

Attached: 1502069067663.gif (257x212, 2.91M)

Because most of the problems aren't "implement this algorithm!" they're "do a bunch of analysis to develop an efficient algorithm!". Shit like being able to generate primes, factor numbers, traverse graphs, is assumed.

>optimizing code to tax your computer less
it's like you want your cpu to atrophy and get lazy

sudo npm install reinventWheel -D

define PRESUBMIT_FROM_REG(x, y) \
push %rdi; \
push %rsi; \
push %rdx; \
push %rcx; \
push %r8; \
push %r9; \
push %r10; \
push %r11; \
mov x, %rdi; \
call y; \
pop %r11; \
pop %r10; \
pop %r9; \
pop %r8; \
pop %rcx; \
pop %rdx; \
pop %rsi; \
pop %rdi; \
ret;

.globl _preSubmitHandlerOfficial
_preSubmitHandlerOfficial:
// Choose the routing
test %al, %al
// Standard routing (for normal calls)
jz __ZN4NGFX39nvVirtualAddressSpace_PreSubmitOfficialEPv
// Wrapped routing (for patched calls)
cmp $1,%al
jz handle_rbx_off
cmp $2,%al
jz handle_r13_off
cmp $3,%al
jz handle_r12_off
// Do we need more registers?
ud2
handle_rbx_off:
PRESUBMIT_FROM_REG(%rbx, __ZN4NGFX39nvVirtualAddressSpace_PreSubmitOfficialEPv)
handle_r13_off:
PRESUBMIT_FROM_REG(%r13, __ZN4NGFX39nvVirtualAddressSpace_PreSubmitOfficialEPv)
handle_r12_off:
PRESUBMIT_FROM_REG(%r12, __ZN4NGFX39nvVirtualAddressSpace_PreSubmitOfficialEPv)

.globl _orgSubmitHandlerOfficial
_orgSubmitHandlerOfficial:
// This is the prologue we patched out
push %rbp
mov %rsp, %rbp
// Jump to the original code
mov _orgVaddrPresubmitOfficial(%rip), %rax
jmp *%rax

A prime is only divisible by 1 and itself. So if you divide i by each j < i and the count is exactly 2, then it's prime.

>this again

stackoverflow.com/questions/18833759/python-prime-number-checker

if num == 1
//prime
else if num == 2
//prime
else if num == 4
//prime
else if num == 7
//prime
else if num == 11
//prime
else if num == 13
//prime
else if num == 17
//prime
else if num == 19
//prime
else if num == 23
//prime
else if num == 29
//prime
else if num == 31
//prime
else if num == 37
//prime
else if num == 41
//prime
else if num == 43
//prime
else if num == 47
//prime
else if num == 53
//prime
else if num == 59
//prime
else if num == 61
//prime
else if num == 67
//prime


etc

repeat for however many numbers you need

^dis^

Modulo is your friend for useless whiteboard/homework algorithms

Attached: l4bN_f-maxage-0_s-200x150.gif (200x150, 646K)

I was just about to ask about this

I dont understand the reason people are nesting loops

if n mod 2 AND n mod 3 both don't equal 0 isn't that all you need?

any number can be made up of different primes multiplied together, a prime factorization.
there are more primes than 2 and 3

>25%2 and 3 !=0
>25 is a prime

Going off of what what about an example of something that satisfies
! = 0 when n % 2, n%3, and n%5

I wrote a sieve 10 years ago when I was fucking around with C99. It probably sucks, judge it all you like. I've condensed it here to remove argument checking and other fluff.

#include
#include
#include
#include

_Bool isNumeric (char[]);

int main (int argc, char *argv[])
{
int m, index;
unsigned long size;
_Bool *primes;

size = atoi (argv[1]);

// Allocate a block of memory and zero out all bits
primes = (_Bool *) calloc (size, sizeof(*primes));

// Main loop
printf ("Calculating primes for digits 0 -> %i...\n\n", size);
for (index = 2; index < size; index++)
{
if (primes[index] == 0)
{
printf ("%i\n", index);
for (m = 2 * index; m < size; m += index)
primes[m] = 1;
}
}
free (primes);
return 0;
}

Just do a limited sieve of Eratosthenes...
Why do I come to Jow Forums anymore

Nevermind, I misread what you wrote, I understand now

>average computer science kids
>knowing sieve
If there's one thing they're good at , it's poor reinventing of a wheel they didn't know existed

Here's a sieve I wrote when playing around with Julia:

function basicsieve(n)::BitVector
sieve = trues(n)
sieve[1] = false
last = Int(ceil(sqrt(n)))
@inbounds for p in 2:last
if sieve[p]
for i in p*p:p:n
sieve[i] = false
end
end
end
sieve
end

function sievesegment(sievedprimes,sieve,start) # starts sieving at start + 1
last = min(Int(ceil(sqrt(start + length(sieve)))) , length(sievedprimes))
@inbounds for p in 2:last
if sievedprimes[p]
for i in (p - start%p):p:length(sieve)
sieve[i] = false
end
end
end
end


using ResumableFunctions


@resumable function segmentedsieve(n::Int, p::Int = 2)
segmentstart = 0
segmentend = Int(ceil(sqrt(n)))
initialsieve = basicsieve(segmentend)
segment = copy(initialsieve)

while p

do... do you think that every composite number is divisible by 2,3,5,7, or 9?

www.cyberian.me/Programming/python look for the crypto library in there. It has a sieve of eratosthenes that does just what you're looking for

void rangeprimeprint(size_t a, size t b){
if(a>b) return;
vector notprime(b+1);
notprime[0]=notprime[1]=true;
for(size_t i = 2; i

Not him but I fell for the same thing, the thought process is (without testing) there are only 10 numbers, we know any number ending in 2,4,6,8, and 10 are automatically eliminated for being even

mod 2 eliminates most numbers multiplied by themselves
mod 3 same for but three times
mod 5 catches fives obviously
mod 7 is a prime without going over 9
mod 9... I don't know why he did this, I wouldn't have, mod 3 catches this
mod 11 while it is over ends in 1 which "looks new"

13, 17, and 19 are prime and end, this is where the assumption sets in because it seems like it'll catch

Of course if you actually test it with something like 17*17 you get a false positive.

TL:DR he doesn't believe it, he didn't care enough to test like myself

>i += 5
that should be += 6

def SieveOfEratosthenes(size):
array = [True] * size
for each in xrange(3,int(sqrt(size))+1,2):
if array[each]:
array[each*each::2*each]=[False]*((size-each*each-1)/(2*each)+1)
return [2] + [each for each in xrange(3,size,2) if array[each]]

>eliminates most numbers multiplied by themselves

made an error, this was supposed to say all, and added to themselves

oh my god

i cant into code but i know about the sieve of erastothenes.

knowing the actual problem is half the battle. code monkeying is the other half

dumb drumpfposter

algolist.net/Algorithms/Number_theoretic/Sieve_of_Eratosthenes

POSIX compliant:
#!/bin/sh

is_prime() {
local i=1 p=0
while [ $i -le $1 ]; do
if [ $(($1%$i)) -eq 0 ]; then
p=$(($p+1))
fi
i=$(($i+1))
done
[ $p -eq 2 ] && return 0
return 1
}

foo() {
local i=$1
while [ $i -le $2 ]; do
is_prime $i && echo $i
i=$(($i+1))
done
}

foo 1 100

Like The C Programming Language by Dennis Ritchie (the creator of C) and Brian Kernighan.