Factorials of large numbers - c

How to compute factorial of numbers like 300 as the output is not even in bound of unsigned long long int ?Please help.
#include<stdio.h>
#include<stdlib.h>
unsigned long long int factorial(int number) {
unsigned long long int temp;
if(number <= 1) return 1;
temp = (number * factorial(number - 1));
return temp;
}
int main(){
int t,k,i,a[100001];
unsigned long long int sum[100001];
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d",&a[i]);
}
for(k=0;k<t;k++){
sum[k]=0;
for(i=0;i<=a[k];i++){
sum[k] += ((factorial(a[k])/(factorial(i)*factorial(a[k]-i)))%3);
//printf("%d\n",sum[k]);
}}
for(i=0;i<t;i++){
printf("%llu\n",sum[i]);
}
return 0;
}
I tried this but it halt at only 60!.

You can try this for Factorials of large numbers :
#include<iostream>
#include<cstring>
int max = 5000;
void display(int arr[]){
int ctr = 0;
for (int i=0; i<max; i++){
if (!ctr && arr[i]) ctr = 1;
if(ctr)
std::cout<<arr[i];
}
}
void factorial(int arr[], int n){
if (!n) return;
int carry = 0;
for (int i=max-1; i>=0; --i){
arr[i] = (arr[i] * n) + carry;
carry = arr[i]/10;
arr[i] %= 10;
}
factorial(arr,n-1);
}
int main(){
int *arr = new int[max];
std::memset(arr,0,max*sizeof(int));
arr[max-1] = 1;
int num;
std::cout<<"Enter the number: ";
std::cin>>num;
std::cout<<"factorial of "<<num<<"is :\n";
factorial(arr,num);
display(arr);
delete[] arr;
return 0;
}

Related

Why does my addArrays function get a wedge exit compile code? (UPDATE FIXED)

The rest of my functions work fabulously, however the last function has my goat. The goal of this function is to use pointers to obtain the values of two different arrays and add those values to a third array. However, when I run the main method to make the function run, it pauses for a second and provides a wedge exit code that does not work.
I've tried removing the if((sizeof(*ptr1)) == (sizeof(*ptr2)){
---insert code here---
}
from the for loop, however, the problem seems to be just the for loop itself.
//===================================Broken Code========================================
#include <stdio.h>
#define MAXIMUM 1000
int sumArrays(int arr1[], int arr2[]);
int addArrays(int arr1[], int arr2[]);
int main()
{
int arrayOne[MAXIMUM];
int arrayTwo[MAXIMUM];
for(int i = 0; i <= MAXIMUM; i++)
arrayOne[i] = i;
printf("Arrayone %d\n", arrayOne);
for(int j = 0; j <= MAXIMUM; j++)
arrayTwo[j] = j;
printf("ArrayTwo %d\n", arrayTwo);
printf(" The sum of the arrays is : %d\n",sumArrays(arrayOne, arrayTwo));
printf("%d", addArrays(arrayOne, arrayTwo));
return 0;
}
int sumArrays(int arr1[],int arr2[]){
int *ptr_1;
int *ptr_2;
ptr_1 = &arr1[0];
ptr_2 = &arr2[0];
int sum;
for(int i = 0; i < MAXIMUM; i++){
sum += *ptr_1 + i;
sum += *ptr_2 + i;
}
return sum;
}
int addArrays(int arr1[],int arr2[]){
int *ptr1 = &arr1[0];
int *ptr2 = &arr2[0];
int sum = 0;
int i = 0;
int arr3[0];
if(sizeof(*ptr1) == sizeof(*ptr2)){
for(int i = 0; i < MAXIMUM; i++){
sum += *ptr1 +i;
sum += *ptr2 +i;
arr3[i] = sum;
}
}
printf("The value of array3 is %d", arr3);
}
The other function works perfectly, but the addArrays function does a wedge exit and doesn't cooperate.
I expect the addArrays function to take the elements from each array, add them together and assign them to the third array.
Thank you for your time.
UPDATE: WORKING CODE
#include <stdio.h>
#define MAXIMUM 1000
#define ARRAY_SZ(x) (sizeof(x) / sizeof((x)[0]))
int sumArrays(int arr1[], int arr2[], size_t len);
int addArrays(int arr1[], int arr2[], int arr3[], size_t len);
int main()
{
int arrayOne[MAXIMUM];
int arrayTwo[MAXIMUM];
int arrayThree[MAXIMUM];
for(int i = 0; i <= MAXIMUM; i++)
arrayOne[i] = i;
printf("Array One %d\n", ARRAY_SZ(arrayOne));
for(int j = 0; j <= MAXIMUM; j++)
arrayTwo[j] = j;
printf("Array Two %d\n", ARRAY_SZ(arrayTwo));
printf(" The sum of the arrays is : %d\n",sumArrays(arrayOne, arrayTwo, ARRAY_SZ(arrayOne)));
printf("%d", addArrays(arrayOne, arrayTwo, arrayThree, MAXIMUM));
return 0;
}
int sumArrays(int arr1[],int arr2[], size_t len){
int *ptr_1;
int *ptr_2;
ptr_1 = &arr1[0];
ptr_2 = &arr2[0];
int sum = 0 ;
for(int i = 0; i < len; i++){
sum += *ptr_1++;
sum += *ptr_2++;
}
return sum;
}
int addArrays(int arr1[],int arr2[], int result[], size_t len){
int *ptr1 = &arr1[0];
int *ptr2 = &arr2[0];
int *ptr3 = &result[0];
int sum = 0;
int sum2 = 0;
int i = 0;
for(int i = 0; i < MAXIMUM; i++){
sum = *ptr1 ++;
sum += *ptr2 ++;
result[i] = sum;
printf("The result of array 3 is %d\n", *ptr3++);
}
}
Here are some notes:
When you assign/pass/print the and array using the name of the array, you are actually passing the memory location of the first element in the array (a pointer).So when you write:
printf("Arrayone %d\n", arrayOne);
You will see the memory address of the first element of the array being printed. If you would like to print the entire array you will need to loop through it. In this case you would be printing 1000 integers which might be undesirable.
void printArray(int * array, size_t len)
{
while(len--)
{
printf("%d ", *array++);
}
}
To get the number of elements in an array you can do something like this:
sizeof(arrayOne) / sizeof(arrayOne[0])
and you can put it in a macro like this:
#define ARRAY_SZ(x) (sizeof(x) / sizeof((x)[0]))
and call it like this:
ARRAY_SZ(arrayOne);
You cannot get the array size if you are receiving an array in a function (it has decayed to a pointer), instead you should pass the array size to the function too. Here because you initialize the arrays with the size MAXIMUM we don't actually need to calculate the array size, but we can just to show it works.
If you want to return an array (like in addArrays()) you should create an empty array and pass it to the function, then the function can update the array with the result.
When looping through an array you never want to do array[maximum] because the array indices range from 0 to maximum - 1
#include <stdio.h>
#define MAXIMUM 1000
#define ARRAY_SZ(x) (sizeof(x) / sizeof((x)[0]))
int sumArrays(int arr1[], int arr2[]);
int addArrays(int arr1[], int arr2[]);
int main()
{
int arrayOne[MAXIMUM];
int arrayTwo[MAXIMUM];
int arrayThree[MAXIMUM];
for(int i = 0; i < MAXIMUM; i++)
arrayOne[i] = i;
printf("Array one size %d\n", ARRAY_SZ(arrayOne));
for(int j = 0; j < MAXIMUM; j++)
arrayTwo[j] = j;
printf("Array Two size %d\n", ARRAY_SZ(arrayTwo));
printf(" The sum of the arrays is : %d\n",sumArrays(arrayOne, arrayTwo, ARRAY_SZ(arrayOne)));
addArrays(arrayOne, arrayTwo, arrayThree, MAXIMUM);
return 0;
}
int sumArrays(int arr1[],int arr2[], size_t len)
{
int *ptr_1;
int *ptr_2;
ptr_1 = &arr1[0];
ptr_2 = &arr2[0];
int sum;
for(int i = 0; i < len; i++){
sum += *ptr_1 + i;
sum += *ptr_2 + i;
}
return sum;
}
void addArrays(int arr1[], int arr2[], int result[], size_t len){
int *ptr1 = arr1;
int *ptr2 = arr2;
int sum = 0;
int i = 0;
for(int i = 0; i < len; i++){
sum = *ptr1 +i;
sum += *ptr2 +i;
result[i] = sum;
}
}

Why are my median and mean not returning anything?

The following code is trying to find the averages of a set of numbers in C, but the median and the mean both do not return anything. How do I make it so the mean and the median both return a float? Am I returning an invalid value or?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
float mean(int arr[], int size){
int sum = 0;
for(int i = 0; i<size; i++){
sum += arr[i];
}
return ((float)sum/size);
}
int range(int arr[], int size){
int smallest = arr[0];
int largest = arr[0];
for(int i=0; i<size; i++){
if(smallest>arr[i]){
smallest = arr[i];
} if(largest<arr[i]){
largest = arr[i];
}
} int difference = largest - smallest;
return difference;
}
int mode(int arr[], int size){
int maxValue = 0;
int maxCount = 0;
for(int i = 0; i<size; i++){
int count = 0;
for(int j = 0; j<size; j++){
if(arr[j] == arr[i]){
count++;
}
} if(count > maxCount){
maxCount = count;
maxValue = arr[i];
}
} return maxValue;
}
float median(int arr[], int size){
qsort(arr, size, sizeof(int), compare);
float middleOfArray = size/2;
int roundedMiddleOfArray = rint(middleOfArray);
if(ceilf(middleOfArray) == middleOfArray){
return((float)arr[roundedMiddleOfArray]);
}
else{
return((float)arr[roundedMiddleOfArray] - arr[roundedMiddleOfArray-1]);
}
}
int main(){
int array[6] = {1,2,3,4,5,6};
int newMean = mean(array, 5);
int newRange = range(array, 5);
int newMode = mode(array,5);
int newMedian = median(array, 5);
printf("The mean is : %f \n", newMean);
printf("The range is : %d \n",newRange);
printf("The mode is : %d \n",newMode);
printf("The median is : %f \n", newMedian);
return 0;
}
Turns out the way you fix it is by declaring the mean and the median as floats not ints (in main).

Heap Sort C doesn't work when change from array to array unsigned int

My code works properly when all variables and array are defined as int, but if I change the type to unsigned int, it won't work.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
void swap(unsigned int *a, unsigned int *b){
unsigned int temp;
temp=*a ; *a=*b ; *b=temp;
}
void heapify(unsigned int A[], unsigned int i, unsigned int n){
//sort from root i
unsigned int L = 2*i+1;
// node left child
unsigned int R = 2*i+2;
//node right child
unsigned int max = i;
//set max node is root
if(L < n && A[L] > A[max]) max = L;
//if node child > max, set that node is max
if(R < n && A[R] > A[max]) max = R;
if(max != i) {
swap(&A[i], &A[max]);
heapify(A, max, n);//recursive tree with root is node swaped
}
}
void buildHeap(unsigned int A[], unsigned int n){
unsigned int i = n/2 - 1;
for(; i >= 0; i--) heapify(A, i, n);
}
void heapSort(unsigned int A[], unsigned int n){
buildHeap(A, n);
unsigned int i = n-1;
for(; i >= 0; i--){
swap(&A[0], &A[i]);
heapify(A, 0, i);
}
}
void PrintArray(unsigned int A[], unsigned int n){
unsigned int i;
for(i = 0; i < n; i++){
printf("%d ", A[i]);
}
}
int main(){
unsigned int A[]={1,6,8,9,7,1,65,92,2,9,2,5,73,9,1,5};
unsigned int n=sizeof (A)/sizeof(unsigned int);
PrintArray(A,n);
heapSort(A,n);
PrintArray(A,n);
}
The output is stuck at print the old array and nothing happens after that.
What should I do?
The problem is at buildHeap - you set your iterator as unsigned int, and run a loop until i is no longer positive - which in the cash of unsigned int will never happen.
void buildHeap(unsigned int A[], unsigned int n){
unsigned int i = n/2 - 1;
for(; i >= 0; i--) heapify(A, i, n);
}

How to ignore/remove leading zeros?

I am writing a program to add two large numbers in C.
My integer array result holds the sum of the two numbers (which were also stored in arrays).
For example, if the result array is [0,0,3,2] (actual array size is 20)
If 32 is my actual result, how can I display the contents of the result array without the leading zeros ?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BASE 10
void align(int A[],int n);
void add(int A[],int B[], int C[]);
void Invert(int* a, int n);
int main(int argc, char** argv){
char input1[20];
char input2[20];
int size = 20;
int a;
int b;
int num1[20];
int num2[20];
int result[20];
int length1 = strlen(argv[1]);
int length2 = strlen(argv[2]);
int i = 0;
for (i=0;i<length1;i++){
input1[i] = argv[1][i];
}
for (i=0;i<length2;i++){
input2[i] = argv[2][i];
}
a=atoi(input1);
b=atoi(input2);
align(num1,a);
align(num2,b);
add(num1,num2,result);
Invert(result,size);
for (i=0;i<20;i++){
printf("%d",result[i]);
}
return 0;
}
void align (int A[], int n){
int i = 0;
while (n) {
A[i++] = n % BASE;
n /= BASE;
}
while (i < 20) A[i++] = 0;
}
void add (int A[], int B[], int C[]) {
int i, carry, sum;
carry = 0;
for (i=0; i<20; i++) {
sum = A[i] + B[i] + carry;
if (sum >= BASE) {
carry = 1;
sum -= BASE;
} else
carry = 0;
C[i] = sum;
}
if (carry) printf ("overflow in addition!\n");
}
void Invert(int* a, int n)
{
int i;
int b;
for(i=0; i<n/2; i++){
b = a[i];
a[i] = a[n-i-1];
a[n-i-1] = b;
}
}
`
To get the actual digits (I assume that each digit is stored as a byte in an array of 20 bytes, lowest digit at highest index), you do something like this:
int i;
int size = sizeof(thearray) / sizeof(thearray[0]);
/* find first non-0 byte, starting at the highest "digit" */
for (i = 0; i < size - 1; ++i)
if (thearray[i] != 0)
break;
/* output every byte as character */
for (; i < size; i++)
printf("%c", thearray[i] + '0'); /* 0 --> '0', 1 --> '1', etc. */
printf("\n");
You can do this by below code:-
int flag=1;
for(i=0;i<20;i++)
{
if(flag==1&&array[i]!=0)
flag=0;
if(flag!=1)
{
printf("%d",array[i]);
}
}
This will remove all leading zeros.
I propose a solution by using the pointer. The situation where only zero is stored in the array is also handled. I'm more comfortable with the pointer.
int test[20] = {0,0,0,0,1,2,3,4,5,6,7,8,9,0,0,1,2,3,4,5};
int test_bis[20] = {0};
int * ptr_test = test_bis;
int ii = 0;
while( *(ptr_test)== 0 && ii < 20 ) {
ptr_test++;
ii++;
}
if( ii < 20)
do {
printf("%d",*(ptr_test));
ptr_test++;
} while (++ii < 20);
else
printf("0");
Thats for integer array you can modify it accordingly.
for(i=0;i<20;i++){
if(flag==1&&array[i]==0)
{
// just skips until first nonzero
}
else if(flag==1&&array[i]!=0){
flag=0; // when first nonzero comes set flag to 0 and print it
printf("%d",array[i]);
}
else {
printf("%d",array[i]); // after first nonzero simply print it
}
}

prime numbers in a given range

I have to find all the prime numbers between two numbers m and n. (1 <= m <= n <= 1000000000 and n-m <= 100000). I am using sieve of eratosthenes but getting wrong answer. Can anyone help me what is wrong with my code.
#include<stdio.h>
#include<math.h>
int S[100002];
void sieve(long long int m, long long int n)
{
long long int x=sqrt(n);
long long int i,j;
long long int a;
for(i=0;i<=n-m+2;i++)
S[i]=0;
if(m%2==0)
i=m;
else {
i=m+1;
}
for (;i<=n;i+=2){
S[i-m]=1;
}
for (i=3;i<=x;i+=2){
if(i>=m && S[i-m]) continue;
if(i*i>=m)j=i*i;
else {
a = (m-i*i)%(2*i);
if(a==0)j=m;
else
j=m+ (2*i -a);
}
for (;j<=n;j+=2*i){
S[j-m]=1;
}
}
if (m==1)i=1; else i=0;
for (;i<=n-m;i++)
if (!S[i]){
printf("%lld\n",i+m);
}
}
int main(){
int t;
long long int m,n;
scanf("%d\n",&t);
while(t--){
scanf("%lld %lld",&m,&n);
sieve(m,n);
printf("\n");
}
return(0);
}
if(m%2==0)
i=m;
else {
i=m+1;
}
for (;i<=n;i+=2){
S[i-m]=1;
}
Now, what happens if m <= 2? Will 2 be considered prime or not?
You should use loop in main and call prime function.
For performance, I recommend you to avoid using sqrt function because it requires a lot of CPU clocks.
bool isPrime(int number){
if(number < 2) return false;
if(number == 2) return true;
if(number % 2 == 0) return false;
for(int i=3; (i*i)<=number; i+=2){
if(number % i == 0 ) return false;
}
return true;
}
***Change datatype for the range of number (long, long long, etc).
It is the most efficient(Sieve method) way of finding prime number between a range.
Here 1 is not consider as a prime number as a conventionally way.
#include<stdio.h>
#include<string.h>
#define max 10000000
using namespace std;
int main()
{
unsigned long long int i, j, k, m, n;
unsigned long long int* a = new unsigned long long int[max];
scanf("%ul %ul",&m,&n);
for(i = 1;i<=n;i++)
a[i]=i;
a[1] = 0;
for(i=2;(i*i)<=n;i++)
if(a[i]!=0)
for(k=2*i;k<=n;k=k+i)
if(a[k]!=0)
a[k]=0;
for(i =m;i<=n;i++)
if(a[i]!=0)
printf("%ul ",a[i]);
memset(a, 0, sizeof(a));
return 0;
}
#include<stdio.h>
#include<stdlib.h>
void prime(int );
int main(){
int x, end;
printf("Enter end of the range:\n");
scanf("%d", &end);
for(x = 2;x <= end;x++){
prime(x);
}
return 0;
}
void prime(int x){
int j, count = 1;
for(j=2;j <= x;j++){
if(x % j == 0){
count += 1;
//printf("count = %d,x = %d", count, x);
}
}
if(count == 2){
printf("\n%d\n", x);
}
}

Resources