Skip to content

Created C program for 05.Smallest_multiple and CPP Program for 10.Summation_of_primes #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
32 changes: 32 additions & 0 deletions C/05.Smallest_multiple/SanziSen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// C program to find smallest number evenly divisible by all numbers 1 to n
#include<stdio.h>
#include<stdlib.h>

long long int gcd(long long int n1, long long int n2)
{ long long int result,i;
for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
result = i;
}
return result;
}


// Function returns the lcm of first n numbers
long long int lcm(long long n)
{
long long int ans = 1;
for (long long int i = 1; i <= n; i++)
ans = (ans * i)/(gcd(ans, i));
return ans;
}

// Driver program to test the above function
int main()
{
long long n = 20;
printf("%lld, lcm(n));
return 0;
}
44 changes: 44 additions & 0 deletions CPP/10.Summation_of_primes/SanziSen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#include<iostream>
#include<cstring>
using namespace std;

long long int sumprimes(long long int n)
{

bool prime[n + 1];

// Create a boolean array "prime[0..n]"
// and initialize all entries it as true.
// A value in prime[i] will finally be
// false if i is Not a prime, else true.
memset(prime, true, n + 1);

for (long long int p = 2; p * p <= n; p++) {

// If prime[p] is not changed, then
// it is a prime
if (prime[p] == true) {

// Update all multiples of p
for (long long int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}

// Return sum of primes generated through
// Sieve.
long long int sum = 0;
for (long long int i = 2; i <= n; i++)
if (prime[i])
sum += i;
return sum;
}

// Driver code
int main()
{
int n = 2000000;
cout << sumprimes(n);
return 0;
}