Write a code that prints all prime numbers from 1 to 100

write a code that prints all prime numbers from 1 to 100

Attached: 1347696491696.png (314x373, 212K)

Other urls found in this thread:

pi2e.ch/blog/wp-content/uploads/2017/03/pi_dec_1m.txt').text
twitter.com/SFWRedditImages

Do your own fucking homework nigger

Attached: 1546558797052.png (511x671, 238K)

Bitch I am Elon Musk i don't get homeworks i was just trying to test your intelligence

Attached: 1355436748549.png (244x248, 105K)

WScript.Echo "1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97"

beat me to it

no brute forcing

Nigger

If you ever want to be a successful programmer user, you need to be ready to lie, cheat and steal at every turn. Take every short-cut, benchmark to see if a lookup table isn't quicker, etc.

great programmers optimize at the strategic level

>lie, cheat and steal at every turn.
What about Elon musk? He never lied, cheated, or stole anything, yet he's the best programmer in the world right now.

import primes

primes(100)

1 isn't prime though

Attached: 1522708192807.jpg (674x674, 102K)

for i in range(100)
if(i%2==0){
print "nigger"
}
else{
print i
}

Hi guys I have a masters in Computer Science from University of India.

for (var i = 0; i < 100; i++){
if (isDivisibleBy1(i) && isDivisibleByItself(i) && !isDivisibleByAnyNumberBelowOneHundred){
console.log("Prime found!");
console.log(i);
}
}

function isDivisibleBy1(var num){
if (num % 1 == 0) return true;
return false;
}

function isDivisibleByItself(var num){
if (var % var == 0) return true;
return false;
}

function isDivisibleByAnyNumberBelowOneHundred(var num){
for (var i = 2; i < 100; i++){
if (var % i == 0 && i var) return true
}
return false;
}

10 PRINT ALL PRIME NUMBERS FROM 1 TO 100
20 BEEP
30 GOTO 10

isprime 1 = True
isprime n = let fp _ 1 = True
fp n m = n `rem` m /= 0 && fp n (m - 1)
in fp n (n - 1)
primes = [x | x

Attached: 1547013817828.jpg (374x374, 41K)

This is why I hate c++!

That's Haskell

Niggers

Could be done better bitwise rather than bytewise but here's a quick and dirty Eratosthenes' sieve:
#include
#include

#define N 100

int main()
{
int SQRT_N = sqrt(N);
int nums[N] = {0};
int i;
for (i = 2; i

Trivially optimized sieve:
#include

#define LIMIT 100
int main()
{
int i, p, j, limit = (LIMIT - 1) / 2;
char candidates[(LIMIT - 1) / 2] = {0};
if (LIMIT >= 2)
puts("2");
for (i = 0; i < limit; i++) {
if (!(candidates[i])) {
p = i + i + 3;
printf("%d\n", p);
for (j = (p*p - 3) >> 1; j < limit; j += p)
candidates[j] = 1;
}
}
}

here u go bro
from typing import List, Set, Dict, Tuple, Optional
import requests
pi = requests.get('pi2e.ch/blog/wp-content/uploads/2017/03/pi_dec_1m.txt').text
pi=pi.replace('.','')
checked=[]
def is_Prime(n: int) -> bool:
listu : List[int] = []
listy1: List[int] = list(range(0,n+1))
try:
listy1.remove(1)
except:
return False
try:
listy1.remove(n)
except:
return False
for xy in [[x*y for y in listy1] for x in listy1 ]*100000:
listu+=xy
for num in listu:
if n == num:
return False
return True

for slide in range(len(pi)-1):
if int(pi[slide:2+slide]) not in checked:
if is_Prime(int(pi[slide:2+slide])):
print(int(pi[slide:2+slide]))
checked.append(int(pi[slide:2+slide]))

>unoptimized/10

>chad code/10

func isPrime(_ n: Int) -> Bool {
guard n != 2 else { return true }
guard n % 2 == 1 else { return false }
for i in stride(from: 3, to: n - 1, by: 2) {
if n % i == 0 {
return false
}
}
return true
}

let result = (2...100).filter { isPrime($0) }
print(result)

Here's a shitty version, heron's formulae for sqrt approximation, and comparison of odd factors up to sqrt.


printf("2\n"); // Two is prime, who cares
for (int i = 3; i < 100; i+=2)
{
int prime = 1;
int estimate = i / (i / 10 + 1);
int approx_sqrt = (i - (estimate * estimate)) / (2 * estimate) // Heron's formulae
approx_sqrt = (i / (approx_sqrt) + approx_sqrt) / 2; // Converging
approx_sqrt = (i / (approx_sqrt) + approx_sqrt) / 2; // Converging, should be good enough for all values less than 100

// Check odd factors, we've excluded all even numbers in the previous loop
for (int j = 3; j

Am too retarded to format code.

Optimisation is for trannies and only needed for 1000+ bit primes anyway.

Attached: primes.jpg (363x243, 19K)

wtf? this isn't even valid python... doesn't run..

What practical use do any of these number sequences have?
I mean, prime numbers are good for hash tables but you don't need the whole sequence for that.
Why is it relevant to programmers?

>why is cryptography relevant

How is number sequences relevant to cryptography then?

Because they aren't predictable.
You can't just run some function like p(5000) and print out the 5,000th prime number without first calculating every single 4,999 primes that come before it (unless you use some lookup table to expediate the process).

Because there is no equation that can simply predict primes, it means that if a calculation can be done a million different ways and only the way that uses primes is valid, then nobody can create a simple way to solve for those primes, aside from brute forcing.

Okay, fair enough.

for ^100 { .is-prime ?? .print !! 0 }

I'm not very good at this, but good enough!

Attached: 6603b052-e1b1-4259-bf2a-869817d34521..png (720x845, 38K)

based

siri open google and find code for all prime numbers from 1 to 100 in python stack overflow

print(', '.join([str(num) for num in range(1, 101) if [num % x for x in range(1, num+1)].count(0) == 2]))

int main()
{
int prime[100];
for (int i = 0; i < 100; i++) prime[i] = 1;
prime[0] = prime[1] = 0;
prime[2] = prime[3] = 1;
for (int i = 2; i

gay

Attached: E89ABCF35F184EA4AE0A4EE7431051D7.jpg (820x1206, 233K)

Based syntax error poster

ecoh "1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97"

oops, it should be echo

What if it was 1000 instead of 100?

That wasn't part of OP's question.
His answer is 100% correct and 10000x faster than all of the answers posted in this thread, and that is a fact.

Then it would take a long time to type

gets it

Transcript show: (Integer primesUpTo: 100)

1 isn't a prime number.

function primes(n) {
let sieve = Array(n).fill(true)
for (let i = 2; i < Math.sqrt(n); i++) {
if (sieve[i]) {
for (let j = i * i; j < n; j += i) {
sieve[j] = false
}
}
}
return sieve.map((value, i) => value ? i : null).filter(value => value)
}

console.log(primes(100))

function primes(n) {
let sieve = Array(n).fill(true)
for (let i = 2; i < Math.sqrt(n); i++) {
if (sieve[i]) {
for (let j = i * i; j < n; j += i) {
sieve[j] = false
}
}
}
return sieve.map((value, i) => value ? i : null).slice(2).filter(value => value)
}

console.log(primes(100))

actually laughed at your stupid joke

primes 0 100

for( i=1; i < 100; i++)
{printf("%d\n", i);}

RAW

Attached: 1525394675957.jpg (249x249, 8K)

/thread

from math import primes

Attached: 1463163008848.png (4760x4986, 638K)