Categories > WeAreDevs > Hangout >
codewars but code any language
Posted
make a program in any language which calculates how many prime numbers there are in 1 million then run it 100 times then calculate the avg time reply with the screenshot and code if you choose to compete.
winner gets the non existant medal of honor
Cancel
Post
Added
https://media.discordapp.net/attachments/980907747031789638/1121069886316220457/image.png
using System;
using System.Diagnostics;
using System.Threading.Tasks;
class PrimeNumberCounter
{
static void Main()
{
const int iterations = 100;
const int limit = 1000000;
long totalTime = 0;
int primeCount = 0;
for (int i = 0; i < iterations; i++)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
primeCount = CountPrimeNumbers(limit);
stopwatch.Stop();
totalTime += stopwatch.ElapsedMilliseconds;
}
double averageTime = (double)totalTime / iterations;
Console.WriteLine("Average time: " + averageTime + " ms");
Console.WriteLine("Prime numbers found: " + primeCount);
}
static int CountPrimeNumbers(int limit)
{
int primeCount = 0;
object lockObj = new object();
Parallel.For(2, limit + 1, number =>
{
if (IsPrime(number))
{
lock (lockObj)
{
primeCount++;
}
}
});
return primeCount;
}
static bool IsPrime(int number)
{
if (number < 2)
return false;
for (int i = 2; i * i <= number; i++)
{
if (number % i == 0)
return false;
}
return true;
}
}
Cancel
Post
Added
@Alawrpar
https://cdn.discordapp.com/attachments/980907747031789638/1121080431501123647/image.png
using System;
using System.Diagnostics;
using System.Threading.Tasks;
class PrimeNumberCounter
{
static void Main()
{
const int iterations = 100;
const int limit = 1000000;
long totalTime = 0;
int primeCount = 0;
for (int i = 0; i < iterations; i++)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
primeCount = CountPrimeNumbers(limit);
stopwatch.Stop();
totalTime += stopwatch.ElapsedMilliseconds;
}
double averageTime = (double)totalTime / iterations;
Console.WriteLine("Average time: " + averageTime + " ms");
Console.WriteLine("Prime numbers found: " + primeCount);
}
static int CountPrimeNumbers(int limit)
{
bool[] isPrime = new bool[limit + 1];
for (int number = 2; number <= limit; number++)
{
isPrime[number] = true;
}
for (int number = 2; number * number <= limit; number++)
{
if (isPrime[number])
{
for (int k = number * number; k <= limit; k += number)
{
isPrime[k] = false;
}
}
}
int primeCount = 0;
for (int number = 2; number <= limit; number++)
{
if (isPrime[number])
{
primeCount++;
}
}
return primeCount;
}
}
Cancel
Post
Added
@Alawrpar
im gonna fart inb your rusty fahh face
https://cdn.discordapp.com/attachments/980907747031789638/1121084362063085608/image.png
using System;
using System.Diagnostics;
class PrimeNumberCounter
{
static void Main()
{
const int iterations = 1000;
const int limit = 1000000;
long totalTime = 0;
int primeCount = 0;
for (int i = 0; i < iterations; i++)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
primeCount = CountPrimeNumbers(limit);
stopwatch.Stop();
totalTime += stopwatch.ElapsedMilliseconds;
}
double averageTime = (double)totalTime / iterations;
Console.WriteLine("Average time: " + averageTime + " ms");
Console.WriteLine("Prime numbers found: " + primeCount);
}
static int CountPrimeNumbers(int limit)
{
bool[] isComposite = new bool[limit + 1];
for (int number = 2; number * number <= limit; number++)
{
if (!isComposite[number])
{
for (int multiple = number * number; multiple <= limit; multiple += number)
{
isComposite[multiple] = true;
}
}
}
int primeCount = 0;
for (int number = 2; number <= limit; number++)
{
if (!isComposite[number])
{
primeCount++;
}
}
return primeCount;
}
}
Cancel
Post
https://media.discordapp.net/attachments/1074297826219139153/1118491534581567568/Mask_group.png
Replied
I am better
Bow down weaklings
https://media.discordapp.net/attachments/1098921594249814066/1121085474082136154/image.png?width=458&height=58
package main
import (
"fmt"
"time"
)
func countPrimes(limit int) int {
isComposite := make([]bool, limit+1)
for number := 2; number*number <= limit; number++ {
if !isComposite[number] {
for multiple := number * number; multiple <= limit; multiple += number {
isComposite[multiple] = true
}
}
}
primeCount := 0
for number := 2; number <= limit; number++ {
if !isComposite[number] {
primeCount++
}
}
return primeCount
}
func main() {
const iterations = 100
const limit = 1000000
totalTime := 0.0
primeCount := 0
for i := 0; i < iterations; i++ {
startTime := time.Now()
primeCount = countPrimes(limit)
endTime := time.Now()
timeTaken := endTime.Sub(startTime).Milliseconds()
totalTime += float64(timeTaken)
}
averageTime := totalTime / float64(iterations)
fmt.Printf("Average time: %.2f ms\n", averageTime)
fmt.Printf("Prime numbers found: %d\n", primeCount)
}
Cancel
Post
#Road to 15 Rep
https://media.discordapp.net/attachments/1081737726048608379/1088936664941989979/WRDBanner3.png
PC Specs: RTX 4080 OC (16GB), 32 GB 3200MHz, i7-11700k
Replied
@Alawrpar i have found a way to beat your score :)
Cancel
Post
https://media.discordapp.net/attachments/1074297826219139153/1118491534581567568/Mask_group.png
Replied
im gonna beat all of you mortals
Cancel
Post
Added
after hours of trying I give up this is the lowest I could get it, so perish now.
If any of you dookies translate it to your language I will skin you alive
https://media.discordapp.net/attachments/1098921594249814066/1121101756324118679/image.png?width=426&height=62
My amazing method in Go:
Cancel
Post
Added
I am looking at you Alawapr
Comments
Zayn 10 Reputation
Commented
gg /charsssss
SeizureSalad 36 Reputation
Commented
slow smh i am faster
Cancel
Post
#Road to 15 Rep
https://media.discordapp.net/attachments/1081737726048608379/1088936664941989979/WRDBanner3.png
PC Specs: RTX 4080 OC (16GB), 32 GB 3200MHz, i7-11700k
Replied
you guys stink fr i got it down to 2 microseconds or 0.002 ms @B00M
https://cdn.discordapp.com/attachments/967933785272352858/1121122157997596702/image.png
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#define MAX_NUMBER 1000000
#define SEGMENT_SIZE 10000
void segmentedSieve(int n) {
int sqrtN = sqrt(n);
bool *isPrime = (bool *)malloc((sqrtN + 1) * sizeof(bool));
bool *primeFlags = (bool *)malloc(n * sizeof(bool));
if (isPrime == NULL || primeFlags == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
for (int i = 0; i <= sqrtN; i++) {
isPrime[i] = true;
}
for (int i = 0; i < n; i++) {
primeFlags[i] = true;
}
for (int p = 2; p * p <= sqrtN; p++) {
if (isPrime[p]) {
for (int i = p * p; i <= sqrtN; i += p) {
isPrime[i] = false;
}
}
}
for (int low = 0; low <= n; low += SEGMENT_SIZE) {
int high = low + SEGMENT_SIZE - 1;
if (high > n) {
high = n;
}
for (int i = 2; i <= sqrtN; i++) {
if (isPrime[i]) {
int start = (low / i) * i;
if (start < low) {
start += i;
}
if (start == i) {
start += i;
}
for (int j = start; j <= high; j += i) {
primeFlags[j] = false;
}
}
}
if (low == 0) {
primeFlags[0] = false;
primeFlags[1] = false;
}
}
int count = 0;
for (int i = 2; i <= n; i++) {
if (primeFlags[i]) {
count++;
}
}
free(isPrime);
free(primeFlags);
}
int main() {
clock_t start, end;
double cpu_time_used = 0.0;
int runs = 100;
for (int i = 0; i < runs; i++) {
start = clock();
segmentedSieve(MAX_NUMBER);
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
}
double avgTime = cpu_time_used / runs;
printf("Average Time: %f seconds\n", avgTime);
return 0;
}
Comments
pooperlegend 0 Reputation
Commented
@Alawrpar embed fail
https://cdn.discordapp.com/attachments/1064359813175328891/1121133416411316344/image.png
SeizureSalad 36 Reputation
Commented
smh close enough tbh
Cancel
Post
"Questionable intellegence, but I like the mystery" - CubeFaces
https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png
Replied
@SeizureSalad fak you, anyways @Alawrpar
https://cdn.discordapp.com/attachments/980907747031789638/1121122448314749099/image.png
#include <iostream>
#include <vector>
#include <cmath>
#include <chrono>
#include <omp.h>
int countPrimeNumbers(int limit)
{
std::vector<char> isComposite(limit + 1, false);
int sqrtLimit = static_cast<int>(std::sqrt(limit));
#pragma omp parallel for schedule(dynamic)
for (int number = 2; number <= sqrtLimit; number++)
{
if (!isComposite[number])
{
for (int multiple = number * number; multiple <= limit; multiple += (2 * number))
{
isComposite[multiple] = true;
}
}
}
int primeCount = 1;
#pragma omp parallel for schedule(guided) reduction(+: primeCount)
for (int number = 3; number <= limit; number += 2)
{
if (!isComposite[number])
{
primeCount++;
}
}
return primeCount;
}
int main()
{
const int iterations = 1000;
const int limit = 1000000;
long long totalTime = 0;
int primeCount = 0;
omp_set_nested(true);
for (int i = 0; i < iterations; i++)
{
auto startTime = std::chrono::high_resolution_clock::now();
primeCount = countPrimeNumbers(limit);
auto endTime = std::chrono::high_resolution_clock::now();
auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
totalTime += elapsedTime;
}
double averageTime = static_cast<double>(totalTime) / iterations;
std::cout << "Average time: " << averageTime << " ms" << std::endl;
std::cout << "Prime numbers found: " << primeCount << std::endl;
return 0;
}
Comments
Zayn 10 Reputation
Commented
im gonna destroy you
Cancel
Post
Added
@B00M goto the new challenge
Comments
Zayn 10 Reputation
Commented
ok but it doesnt mean ive lost here, I can come back and do it
pooperlegend 0 Reputation
Commented
@B00M yes loll
Cancel
Post
https://media.discordapp.net/attachments/1074297826219139153/1118491534581567568/Mask_group.png
Users viewing this thread:
( Members: 0, Guests: 1, Total: 1 )
Comments
pooperlegend 0 Reputation
Commented
i will beat you just wait you little son of a
0
Zayn 10 Reputation
Commented
just wait I have more power :angry:
0