I have to count number of distinct prime factors over 2 to 100000, Is there any fast method than what I am doing ?
i.e.. 2 has 1 distinct prime factor 2
10 has 2 distinct prime factor (2,5)
12 has 2 distinct prime factor (2,3)
My code :-
#include<stdio.h>
#include<math.h>
typedef unsigned long long ull;
char prime[100000]={0};
int P[10000],fact[100000],k;
void sieve()
{
int i,j;
P[k++]=2;
for(i=3;i*i<1000;i+=2)
{
if(!prime[i])
{
P[k++]=i;
for(j=i*i;j<100000;j+=i+i)
prime[j] = 1;
}
}
for(i=1001;i<100000;i+=2)
if(!prime[i])
P[k++]=i;
}
int calc_fact() {
int root,i,count,j;
fact[1]=fact[2]=fact[3]=1;
for(i=4;i<=100000;i++) {
count=0;
root=i/2+1;
for(j=0;P[j]<=root;j++) {
if(i%P[j]==0)count++;
}
if(count==0) fact[i]=1;
else fact[i]=count;
}
return 0;
}
int main(){
int i;
sieve();
calc_fact();
for(i=1;i<10000;i++) printf("%d ,",fact[i]);
return 0;
}
You can easily adapt the sieve of Erasthotenes to count the number of prime factors a number has.
Here's an implementation in C, along with some tests:
#include <stdio.h>
#define N 100000
static int factorCount[N+1];
int main(void)
{
int i, j;
for (i = 0; i <= N; i++) {
factorCount[i] = 0;
}
for (i = 2; i <= N; i++) {
if (factorCount[i] == 0) { // Number is prime
for (j = i; j <= N; j += i) {
factorCount[j]++;
}
}
}
printf("2 has %i distinct prime factors\n", factorCount[2]);
printf("10 has %i distinct prime factors\n", factorCount[10]);
printf("11111 has %i distinct prime factors\n", factorCount[11111]);
printf("12345 has %i distinct prime factors\n", factorCount[12345]);
printf("30030 has %i distinct prime factors\n", factorCount[30030]);
printf("45678 has %i distinct prime factors\n", factorCount[45678]);
return 0;
}
You can definitely do better by making a sieve of Eratosthenes.
In Python
N = 100000
M = int(N**.5) # M is the floor of sqrt(N)
nb_of_fact = [0]*N
for i in xrange(2,M):
if nb_of_fact[i] == 0: # test wether i is prime
for j in xrange(i,N,i): # loop through the multiples of i
nb_of_fact[j] += 1
for i in xrange(M,N):
if nb_of_fact[i] == 0:
nb_of_fact[i] = 1
At the end of the loop, nb_of_fact[i] is the number of prime factors of i (in particular it is 1 if and only if i is prime).
Older wrong version
N = 100000
nb_of_fact = [1]*N
for i in xrange(2,N):
if nb_of_fact[i] == 1: # test wether i is prime
for j in xrange(2*i,N,i): # loop through the multiples of i
nb_of_fact[j] += 1
Related
I want to find out the function to generate the sequence with the following pattern.
1 2 3 1 1 2 2 3 3 1 1 1 1 2 2 2 2 3 3 3 3 ....
Where the lower bound number is 1 upper number bound number is 3. Each time numbers start from 1 and each number repeats 2 ^ n times, with n starting with 0.
Here it goes, I hope it will help.
#include <iostream>
#include <math.h>
int main(){
for(int n = 0; n < 5;n++){
for(int i = 1; i < 4;i++){
for(int j = 0;j < pow(2,n) ;j++){
std::cout << i;
}
}
}
return 0;
}
Here is a code in C++:
#include <iostream>
#include <cmath>
int main()
{
// These are the loop control variables
int n, m, i, j, k;
// Read the limit
cin >> n;
// Outermost loop to execute the pattern {1..., 2..., 3...} n times
for (i = 0; i < n; ++i)
{
// This loop generates the required numbers 1, 2, and 3
for (j = 1; j <= 3; ++j)
{
// Display the generated number 2^i times
m = pow(2, i);
for (k = 0; k < m; ++k)
{
std::cout << j << ' ';
}
}
}
}
You can use the same logic in any language you choose to implement it.
So I have a grid which is in one form and im trying to get the X and Y.
Is there a formula where I could turn for example 12 into 2,2 or 14 to 2,3
Also is there a name for this type of grid?
static int getX(int z)
{
int count = 0;
int res = 0;
int curr = 0;
for(int temp = z; temp > 0; temp >>= 1)
{
if(count % 2 ==0)
{
res += ((temp & 1) << curr);
curr++;
}
count++;
}
return res;
}
static int getY(int z)
{
int count = 0;
int res = 0;
int curr = 0;
for(int temp = z; temp > 0; temp >>= 1)
{
if(count % 2 ==1)
{
res += ((temp & 1) << curr);
curr++;
}
count++;
}
return res;
}
As Sneftel observed, this looks like a Z-order curve. As such, you can convert coordinates by interleaving binary representations. So your examples are
0 1 0 x=2 0 1 0 x=2
0 1 0 y=2 0 1 1 y=3
001100 p=12 001110 p=14
So to get x and y coordinates from the cell number p, you assign the bits of p alternatingly to x and y. This kind of bit arithmetic is pretty hard to express using elementary arithmetic operations, and there is no generally recognized formula symbol for this that I'm aware of, but the idea is quite simple.
The program needs to measure the maximum number of modulus operations it took to calculate the GCD of 2 integers at each value of "i" from 8 to n.
For example, if i = 8, I have to find the combination of (a,b) that took the most modulus operations to calculate the GCD.
I have to calculate GCD for all values of 'a' (1-i) for every value of 'b' between (1-i) and do this at every value of "i" until it reaches n.
For example, if n = 15, the output must look like this:
At i = 8; GCD (5, 8) = 1 took 4 modulus operations
At i = 9; GCD (5, 8) = 1 took 4 modulus operations
At i = 10; GCD (5, 8) = 1 took 4 modulus operations
At i = 11; GCD (5, 8) = 1 took 4 modulus operations
At i = 12; GCD (5, 8) = 1 took 4 modulus operations
At i = 13; GCD (8, 13) = 1 took 5 modulus operations
At i = 14; GCD (8, 13) = 1 took 5 modulus operations
At i = 15; GCD (8, 13) = 1 took 5 modulus operations
Here's my code so far because I'm so confused on the implementation:
class Euclidean
{
public:
//Finds the GCD using modulus operations until the remainder is 0
int calcGCD(int x , int y)
{
int remainder;
while(y != 0)
{
remainder = x % y;
x = y;
y = remainder;
}
return x;
}
//Calculates the maximum amount of oprations required to calculate GCD
//at (a,b) for every i from 8 to n
int CalculateMaxOperations(int n)
{
int GCD;
int a;
int b;
for(i = 8; i <= n; i++)
{
for(b = 1; b <= i; b++)
{
if( b <= i)
{
for(a = 1; a <= i; a++)
{
if(a <= i)
{
GCD = calcGCD(a, b);
}
}
}
}
}
}
//Print statement reveals the set (a,b) that took the most operations to calculate
//the GCD at every single value 'i' until it eventually reaches 'n.'
void PrintResults(int c, int d, int operations)
{
cout << "At i = " << i << ";" << " GCD (" << c << "," << d << ")" << " = " <<
calcGCD (c, d) << " took " << operations << " operations " << endl;
}
private:
int i;
};
int main()
{
Euclidean e;
int x = e.CalculateMaxOperations(15);
return 0;
}
I am having a trouble in calling the user defined functions in the main program, using unix. the program is executing only for the number generation in the main program. but when i call the predefined function . the output retrieved is incorrect. Can someone please correct me where i have done it wrong
My main program states as
#include <stdio.h>
#include <stdlib.h>
void sort1(int []);
int main(void) {
int array[100];
int i , j;
printf("ten random numbers in [1,1000]\n");
for (i = 1; i <= 10; i++)
{
generate random numbers
}
printf("The list of Hundred random numbers are \n");
for (j = 1; j <= 10; j++ )
{
//to print the random numbers
}
sort1(array);
return 0;
}
//this is my user defined function: sort1.c
include <stdio.h>
int sort1(int a[])
{
int array[100], i, d, swap, e=10;
// algortithm
}
}
printf("Sorted list in ascending order:\n");
for ( i= 0 ; i< e ; i++ )
printf("%d\n", array[i]);
}
I get the output as
ten random numbers in [1,1000]
The list of Hundred random numbers are
--This gives correct output
Sorted list in ascending order:
1
-1442229816
0
-1444472964
#include <stdio.h>
#include <stdlib.h>
void sort1(int []);
int main(void) {
int array[100];
int i;
printf("ten random numbers in [1,1000]\n");
for (i = 1; i <= 10; i++)
{
array[i] = rand()%1000 + 1;
}
printf("The list of Hundred random numbers are \n");
for (i = 1; i <= 10; i++ )
{
printf("Element[%d] = %d\n", i, array[i] );
}
//Up to here it's ok but you have set the values in the positions 1-10 of the array so keep consistent with it
sort1(array);
printf("Sorted list in ascending order:\n");
for ( i= 1 ; i<= 10 ; i++ )
printf("%d\n", array[i]);
return 0;
}
void sort1(int a[])
{
int i,swap,sorted=0;
while(sorted==0){ //flag to know if array is sorted 0 means not sorted.
sorted=1; // we set the flag to sorted at start of sweep
for (i= 1 ; i<= (10-1); i++) //sweep through array
{
if (a[i] > a[i+1]) //check if sorted
{
swap = a[i];
a[i] = a[i+1];
a[i+1] = swap;
sorted=0; //if not sorted set flag to 0 and swap
}
}
}
}
Main problems in your code:
1) array[100] is not initialized in sort1 function.
2) I do not understand your sorting algorithm but in any case you are checking for values of a[0] which are not initialized so take care of the array positions you use each time and be consistent with it.
3) function prototype does not match
I have to convert a natural number to binary but using recursion. I did but without recursion:
int main (){
int n,pot,bin;
printf("Digite o Numero:\n");
scanf("%d",&n);
pot=1;
bin=0;
while (n>0){
bin+=(n%2)*pot;
pot*=10;
n= n/2;
}
printf ("%d",bin);
getch();
return 0;
}
something like (ASSUMING n is positive!):
void getBin(uint n, int pot, int* bin) {
*bin += (n%2)*pot;
n /= 2;
if (n <= 0) {
return;
}
getBin(n, pot * 10, bin);
}
Just to be on the safe side this does not convert a positive number to binary. It creates a number that, when displayed in base 10, looks like the argument in base 2.
A better solution would be to convert the number to char* in a given base.
int getBin(int n) {
return getBinHelper(n, 1, 0);
}
int getBinHelper(int n, int e, int acc) {
return n == 0 ?
acc :
getBinHelper( n/2, e*10, n&1 ? acc+e : acc);
}