monty hall %50 - %50 - c

i am trying to simulate monty hall problem. i didn't realized any problem but i recieve approximately %50 %50 output. i know that there are explanations but i couldnt understand these
please help me
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define win 1
#define lose 0
#define yes 1
#define no 0
int main(){
//i assume that prize is behind the first door
srand(time(NULL));
int lose_counter = 0;
int win_counter = 0;
for(int i = 1;i <= 10000;i++){
int game_status;
int chosen_door = rand() % 3 + 1;
int choice;
//first step that i chose a door
if(chosen_door == 1){
game_status = win;
}
else if(chosen_door == 2){
game_status = lose;
}
else if(chosen_door == 3){
game_status = lose;
}
//host says "do you want to change your door"
choice = rand() % 2;
if(choice == yes){
if(chosen_door == 1 ){//this is the case i have chosen
//first door and change it after question
game_status = lose;
}
if(chosen_door == 2 ){
game_status = win;
}
if(chosen_door == 3 ){
game_status = win;
}
}
if(game_status == win){
win_counter++;
}
else if(game_status == lose){
lose_counter++;
}
}
printf("win: %d\nlose: %d\n",win_counter,lose_counter);
return 0;
}

The chances to win when not switching the doors are 1/3.
If you switch the chances increase to 2/3.
Your code takes samples of 50% switching and 50% not switching, hence the average of 1/3 and 2/3 comes up as 0.5.

The odds of winning are 2/3 if you switch.
Using
choice = 1; // Switch
results in win in about 2/3 of your trials as expected.
The odds of winning are 1/3 if you don't switch.
Using
choice = 0; // Keep
results in win in about 1/3 of your trials as expected.
If you randomly decide to switch or keep, the odds of winning are
( 1/2 * 2/3 ) + ( 1/2 * 1/3 ) = 1/2
Using
choice = rand() % 2;
results in win in about 1/2 of your trials as expected.
Fixed:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
// I assume that prize is behind the first door
srand(time(NULL));
unsigned N = 10000;
unsigned win_if_keep_counter = 0;
unsigned win_if_switch_counter = 0;
for (unsigned i=N; i--; ) {
int chosen_door = rand() % 3 + 1;
if (chosen_door == 1) {
// Monte open door 2 or 3.
// You win if you keep door 1.
++win_if_keep_counter;
}
else if (chosen_door == 2) {
// Monte open door 3.
// You win if you switch to door 1.
++win_if_switch_counter;
}
else if (chosen_door == 3) {
// Monte open door 2.
// You win if you switch to door 1.
++win_if_switch_counter;
}
}
printf("win if keep: %.1f%%\n", ((double)win_if_keep_counter)/N*100);
printf("win if switch: %.1f%%\n", ((double)win_if_switch_counter)/N*100);
}

Related

finding how many times number2 is showing in number1

I am solving an exercise in C and I got stuck. I don't know the logic of the code to get to my solution. For example we enter 2 numbers from input let the numbers be 123451289 and 12 and I want to see how many times number 2 is showing at number 1 (if this is confusing let me know). For the numbers earlier the program outputs 2. I tried solving it here is my code:
#include <stdio.h>
int main() {
int num1, num2, counter = 0;
scanf("%d%d", num1, num2);
if (num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
int copy1 = num1;
int copy2 = num2;
while (copy2 > 0) {
counter++; // GETTING THE LENGHT OF THE SECOND NUMBER
copy2 /= 10;
// lastdigits = copy1 % counter //HERE I WANT TO GET THE LAST DIGITS OF THE FIRST NUMBER
// But it does not work
}
}
My question is how can I get the last digits of the first number according to the second one for example if the second number have 3 digits I want to get the last 3 digits of the first number. For the other part I think I can figure it out.
I must solve this problem WITHOUT USING ARRAYS.
The problem: find all the needles (e.g. 12) in a haystack (e.g. 123451289).
This can be done simply without arrays using a modulus of the needle. For 12, this is 100. That is, 12 is two digits wide. Using the modulus, we can
isolate the rightmost N digits of the haystack and compare them against the needle.
We "scan" haystack repeatedly by dividing by 10 until we reach zero.
Here is the code:
#include <stdio.h>
int
main(void)
{
int need, hay, counter = 0;
scanf(" %d %d", &hay, &need);
// ensure that the numbers are _not_ reversed
if (hay < need) {
int temp = need;
need = hay;
hay = temp;
}
// get modulus for needle (similar to number of digits)
int mod = 1;
for (int copy = need; copy != 0; copy /= 10)
mod *= 10;
// search haystack for occurences of needle
// examine the rightmost "mod" digits of haystack and check for match
// reduce haystack digit by digit
for (int copy = hay; copy != 0; copy /= 10) {
if ((copy % mod) == need)
++counter;
}
printf("%d appears in %d exactly %d times\n",need,hay,counter);
return 0;
}
UPDATE:
I'm afraid this does not work for 10 0. –
chqrlie
A one line fix for to the modulus calculation for the 10/0 case. But, I've had to add a special case for the 0/0 input.
Also, I've added a fix for negative numbers and allowed multiple lines of input:
#include <stdio.h>
int
main(void)
{
int need, hay, counter;
while (scanf(" %d %d", &hay, &need) == 2) {
counter = 0;
// we can scan for -12 in -1237812
if (hay < 0)
hay = -hay;
if (need < 0)
need = -need;
// ensure that the numbers are _not_ reversed
if (hay < need) {
int temp = need;
need = hay;
hay = temp;
}
// get modulus for needle (similar to number of digits)
int mod = need ? 1 : 10;
for (int copy = need; copy != 0; copy /= 10)
mod *= 10;
// search haystack for occurences of needle
// examine the rightmost "mod" digits of haystack and check for match
// reduce haystack digit by digit
for (int copy = hay; copy != 0; copy /= 10) {
if ((copy % mod) == need)
++counter;
}
// special case for 0/0 [yecch]
if ((hay == 0) && (need == 0))
counter = 1;
printf("%d appears in %d exactly %d times\n", need, hay, counter);
}
return 0;
}
Here is the program output:
12 appears in 123451289 exactly 2 times
0 appears in 10 exactly 1 times
0 appears in 0 exactly 1 times
UPDATE #2:
Good fixes, including tests for negative numbers... but I'm afraid large numbers still pose a problem, such as 2000000000 2000000000 and -2147483648 8 –
chqrlie
Since OP has already posted an answer, this is bit like beating a dead horse, but I'll take one last attempt.
I've changed from calculating a modulus of needle into calculating the number of digits in needle. This is similar to the approach of some of the other answers.
Then, the comparison is now done digit by digit from the right.
I've also switched to unsigned and allow for the number to be __int128 if desired/supported with a compile option.
I've added functions to decode and print numbers so it works even without libc support for 128 bit numbers.
I may be ignoring [yet] another edge case, but this is an academic problem (e.g. we can't use arrays) and my solution is to just use larger types for the numbers. If we could use arrays, we'd keep things as strings and this would be similar to using strstr.
Anyway, here's the code:
#include <stdio.h>
#ifndef NUM
#define NUM long long
#endif
typedef unsigned NUM num_t;
FILE *xfin;
int
numget(num_t *ret)
{
int chr;
num_t acc = 0;
int found = 0;
while (1) {
chr = fgetc(xfin);
if (chr == EOF)
break;
if ((chr == '\n') || (chr == ' ')) {
if (found)
break;
}
if ((chr >= '0') && (chr <= '9')) {
found = 1;
acc *= 10;
chr -= '0';
acc += chr;
}
}
*ret = acc;
return found;
}
#define STRMAX 16
#define STRLEN 100
const char *
numprt(num_t val)
{
static char strbuf[STRMAX][STRLEN];
static int stridx = 0;
int dig;
char *buf;
buf = strbuf[stridx++];
stridx %= STRMAX;
char *rhs = buf;
do {
if (val == 0) {
*rhs++ = '0';
break;
}
for (; val != 0; val /= 10, ++rhs) {
dig = val % 10;
*rhs = dig + '0';
}
} while (0);
*rhs = 0;
if (rhs > buf)
--rhs;
for (char *lhs = buf; lhs < rhs; ++lhs, --rhs) {
char tmp = *lhs;
*lhs = *rhs;
*rhs = tmp;
}
return buf;
}
int
main(int argc,char **argv)
{
num_t need, hay, counter;
--argc;
++argv;
if (argc > 0)
xfin = fopen(*argv,"r");
else
xfin = stdin;
while (1) {
if (! numget(&hay))
break;
if (! numget(&need))
break;
counter = 0;
// we can scan for -12 in -1237812
if (hay < 0)
hay = -hay;
if (need < 0)
need = -need;
// ensure that the numbers are _not_ reversed
if (hay < need) {
num_t temp = need;
need = hay;
hay = temp;
}
// get number of digits in needle (zero has one digit)
int ndig = 0;
for (num_t copy = need; copy != 0; copy /= 10)
ndig += 1;
if (ndig == 0)
ndig = 1;
// search haystack for occurences of needle
// starting from the right compare digit-by-digit
// "shift" haystack right on each iteration
num_t hay2 = hay;
for (; hay2 != 0; hay2 /= 10) {
num_t hcopy = hay2;
// do the rightmost ndig digits match in both numbers?
int idig = ndig;
int match = 0;
for (num_t need2 = need; idig != 0;
--idig, need2 /= 10, hcopy /= 10) {
// get single current digits from each number
int hdig = hcopy % 10;
int ndig = need2 % 10;
// do they match
match = (hdig == ndig);
if (! match)
break;
}
counter += match;
}
// special case for 0/0 et. al. [yecch]
if (hay == need)
counter = 1;
printf("%s appears in %s exactly %s times\n",
numprt(need), numprt(hay), numprt(counter));
}
return 0;
}
Here's the program output:
12 appears in 123451289 exactly 2 times
123 appears in 123451289 exactly 1 times
1234 appears in 123451289 exactly 1 times
1 appears in 123451289 exactly 2 times
0 appears in 10 exactly 1 times
0 appears in 0 exactly 1 times
1000000000 appears in 1000000000 exactly 1 times
2000000000 appears in 2000000000 exactly 1 times
This looks along the lines of what you're attempting.
You can use the pow() function from math.h to raise 10 to the power of how many digits you need for your modulus operation.
Compile with -lm or make your own function to calculate 10^num_digits
#include <stdio.h>
#include <math.h>
int main() {
int x = 123456789;
double num_digits = 3.0;
int last_digits = x % (int)pow(10.0, num_digits);
printf("x = %d\nLast %d Digits of x = %d\n", x, (int)num_digits, last_digits);
return 0;
}
Outputs:
x = 123456789
Last 3 Digits of x = 789
I think you are trying to ask :- if number1 = 1234567 and number2 = 673, then, length of number2 or number2 has 3 digits, so, you now want the last 3 digits in number1, i.e, '456', if I'm not wrong.
If that is the case, then, what you did to find the number of digits in num2 is correct, i.e,
while (copy2>0) {
counter++; // GETTING THE LENGHT OF THE SECOND NUMBER
copy2/=10;
}
you can do the same for number1 and find out its number of digits, then you can compare whether the number of digits in number2 is less than that in number1. Ex, 3 is less than number of digits in number1, so you can proceed further. Let's say number of digits in number1 is 7 and you want the last 3 digits, so you can do iterate over the digits in number1 till count of digits in number2 and pop out each last digit and store them in an array.
The code:
#include <stdio.h>
int main()
{
int num1,num2;
int count1 = 0, count2 = 0;
scanf("%d",&num1);
scanf("%d",&num2);
if(num1<num2){
int temp = num1;
num1 = num2;
num2 = temp;
}
int copy1 = num1;
int copy2 = num2;
while (copy1>0)
{
count1++;
copy1/=10;
}
while (copy2>0)
{
count2++;
copy2/=10;
}
// printf("num1 has %d digits and num2 has %d digits\n", count1, count2);
if (count1 >= count2)
{
int arr[count2];
int x = count2;
int p = num1;
int i = 0;
while (x > 0)
{
arr[i++] = p%10;
x --;
p/=10;
}
for (int j = 0; j < i; j++)
{
printf("%d ", arr[j]);
}
}
return 0;
}
output : 8 7 6
let's say, num1 = 12345678, num2 = 158, then arr = {8,7,6}.
You must determine the number of digits N of num2 and test if num1 ends with num2 modulo 10N.
Note these tricky issues:
you should not sort num1 and num2: If num2 is greater than num1, the count is obviously 0.
num2 has at least 1 digit even if it is 0.
if num1 and num2 are both 0, the count is 1.
if num2 is greater then INT_MAX / 10, the computation for mod would overflow, but there can only be one match, if num1 == num2.
it is unclear whether the count for 1111 11 should be 2 or 3. We will consider all matches, including overlapping ones.
to handle larger numbers, we shall use unsigned long long instead of int type.
Here is a modified version:
#include <limits.h>
#include <stdio.h>
int main() {
int counter = 0;
unsigned long long num1, num2;
if (scanf("%llu%llu", &num1, &num2) != 2) {
printf("invalid input\n");
return 1;
}
if (num1 == num2) {
/* special case for "0 0" */
counter = 1;
} else
if (num1 > num2 && num2 <= ULLONG_MAX / 10) {
unsigned long long copy1 = num1;
unsigned long long mod = 10;
while (mod < num2) {
mod *= 10;
}
while (copy1 > 0) {
if (copy1 % mod == num2)
counter++;
copy1 /= 10;
}
}
printf("count=%d\n", counter);
return 0;
}
Note that leading zeroes are not supported in either number: 101 01 should produce a count of 1 but after conversion by scanf(), the numbers are 101 and 1 leading to a count of 2. It is non trivial to handle leading zeroes as well as numbers larger than ULLONG_MAX without arrays.
This was the answer that i was looking for but thank you all for helping :)
#include <stdio.h>
#include <math.h>
int main(){
int num1,counter1,counter2,num2,temp,digit,copy1,copy2;
scanf("%d%d",&num1,&num2);
if(num1<num2){
temp = num1;
num1 = num2;
num2 = temp;
}
copy1 = num1;
copy2 = num2;
counter1 = counter2 = 0;
while (copy2>0) {
counter1++;
copy2/=10;
}
counter1 = pow(10,counter1);
if(num1> 1 && num2>1)
while (copy1>0) {
digit = copy1%counter1;
if(digit==num2){
counter2++;
}
copy1/=10;
} else{
if(num2<1){
while (copy1>0) {
digit = copy1%10;
if(digit==copy2){
counter2++;
}
copy1/=10;
}
}
}
printf("%d",counter2);
}

Why is this C program crashes on windows and works fine on linux?

I wrote a program to find the harmonic numbers
(the n-th harmonic number is the sum of the reciprocals of the first n natural numbers)
of sequence of numbers. My program takes its input as command line arguments and prints the harmonic numbers in the table format.
As an example This is how it works:
C:\Users\EDDiE\c>har 10 30 5
10 2.9289682539682538
15 3.3182289932289937
20 3.5977396571436819
25 3.8159581777535068
30 3.9949871309203906
10(argv [1]) = starting number,
30(argv[2]) = ending number,
5(argv[3]) = step to advance.
In my windows 8 machine this program crashes at the number 43429
Then I execute on an online c compiler it is a linux environment ( "I think", I'm not a linux user ) And it works fine then.
Here is my program:
har.c
#include <stdio.h>
#include <stdlib.h>
double harmonic(int);
int main(int argc, char *argv[])
{
int j;
int start = 1, end = 10, step = 1;
switch(argc) {
case 1:
break;
case 2:
end = atoi(argv[1]);
break;
case 3:
start = atoi(argv[1]);
end = atoi(argv[2]);
break;
case 4:
start = atoi(argv[1]);
end = atoi(argv[2]);
step = atoi(argv[3]);
break;
}
for (j = start; j <= end; j += step)
printf("%7d %3.20g\n", j, harmonic(j));
return 0;
}
double harmonic(int n)
{
//double H;
if (n == 1 || n == 0)
return 1.0;
else
return (1.0 / (double) n) + harmonic(n - 1);
//return H;
}
I need to know why this program crashes at windows environment.
Is there anything I need to modify in the code (or in my system)?
As already been stated in the comments you're most probably experiencing a stack-overflow. There's hope though, your recursion can be transformed easily to an iteration as follows:
double harmonic(int n) {
if(n == 0) return 1.0;
double res = 0.0;
while(n > 0) {
res += (1.0 / (double) n);
--n;
}
return res;
}
This way your program will work for big n.

Restaurant simulation

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define Empty 1
#define Full 0
float ChanceOfArrival (float CustomersPerMinute)
{
float CustomersArrivalChance;
int i;
/*Customers arriving per second */
CustomersArrivalChance = (CustomersPerMinute / 60) * 100;
printf ("The chance of customers arriving is: %0.3f%%\n", CustomersArrivalChance);
/* each 10 minute interval */
for (i = 0; i <= 18; i++)
{
intervals (CustomersArrivalChance);
}
return CustomersArrivalChance;
}
int intervals (CustomersArrivalChance)
{
int totalCustomers = 0, totalWait = 0, queue = 0, SecondsInterval, waitingCustomers = 0;
int Cash1Salad, Cash1Buger, Cash2Salad, Cash2Burger;
int Cash1 = 1, Cash2 = 1;
int cointoss;
int x, Empty1, Empty2;
int CustomersServed = 0;
float RatePerMinute = 0, AverageWait = 0;
static int intervalNumber;
srand(time(NULL));
/*What could possibly happen every second in a 10 minute interval */
for (SecondsInterval = 0; SecondsInterval <= 600; SecondsInterval++)
{
x = rand() % 101;
if (CustomersArrivalChance >= x)
{
/*Customers Arrive this second */
totalCustomers++;
queue++;
/*Choosing a cash at random */
cointoss = rand()%2;
if (queue > 0)
{
/* Cash 1 is open cash 2 is busy so the customer goes to cash 1 and chooses
at random what they want to eat */
if ((Cash1 == Empty) && (Cash2 != Empty) || (cointoss == 1) )
{
Cash1 = Full;
queue--;
switch ((rand()%2))
{
case 0:
Cash1Salad = rand()% 66 + 55;
totalWait = totalWait + Cash1Salad;
Empty1 = Cash1Salad;
CustomersServed++;
break;
case 1:
Cash1Buger = rand()% 130 + 111;
totalWait = totalWait + Cash1Buger;
Empty1 = Cash2Burger;
CustomersServed++;
break;
}
}
/* Cash 1 is busy cash 2 is open customer goes to cash 2 and chooses what they want */
else if (Cash2 = Empty)
{
Cash2 = Full;
queue--;
switch ((rand()%2))
{
case 0:
Cash2Salad = rand()% 75 + 65;
totalWait = totalWait + Cash2Salad;
Empty2 = Cash2Salad;
CustomersServed++;
break;
case 1:
Cash2Burger = rand()% 140 + 121;
totalWait = totalWait + Cash2Burger;
Empty2 = Cash2Burger;
CustomersServed++;
break;
}
}
/*Both cashes are busy so the customer has to wait until one cash opens */
else
{
totalWait++;
waitingCustomers++;
}
/*Clearing Cash 1 if someone went there */
if (Empty1 > 0)
{
Empty1--;
}
/*empty1 is equal to 0 then cash 1 is empty */
else
{
Cash1 = Empty;
}
/*Clearing cash 2 is someone went there */
if (Empty2 > 0)
{
Empty2--;
}
/*empty2 is equal to 0 then cash 2 is empty */
else
{
Cash2 = Empty;
}
}
}
else
{
/*nothing happens because no customer showed up */
}
}
intervalNumber++;
AverageWait = ((totalWait*1.0)/ (totalCustomers));
printf ("The average waiting time per customer in seconds is %0.2f in the interval %d\n\n", AverageWait, intervalNumber);
printf ("The total customers that arrived in the interval %d is %d\n\n", intervalNumber, totalCustomers);
}
int main (void)
{
float CustomersPerMinute;
printf ("Enter in the number of customers you want to arrive per minute:\n");
scanf ("%f", &CustomersPerMinute);
ChanceOfArrival(CustomersPerMinute);
return 0;
}
Hi, I have this program that is suppose to simulate a restaurant that only serves a salad or burger, there are only two cashiers and only one line up that the customers can line up in to get served.
I am not sure why it is not working but as everything makes logical sense I believe, but when i run this program is just prints out the average waiting time as it is suppose to.
But the problem is that the average wait time that is printed is the same in every interval. Which is not what is I want.
Logically I think the average wait time should be different in each interval because customers are being generated at random. However this is not the case as the average wait time is always the same, how can i fix this?
As well the total number of customers is the same for each interval when is should be different because the customers are generated at random, how can i fix this problem as well?
It looks like you are looping srand. So it may give you the same rand() results at a given second.
Stack overflow is not a debuging forum though ;) .

Prime Numbers Optimization in C [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I try to print the prime numbers; 2 to 1 million. But nothing printed on the console. Could you check my code? And how can I be this code more optimized?
Here's my code:
#include <stdio.h>
#include <math.h>
main()
{
int num, sr, num2;
for (num = 2; num <= 1000000; num++) {
sr = (int) sqrt(num);
for (num2 = 2; sr % num2 != 0; num2++) {
if (sr == num2) {
printf("%d\n", sr);
}
}
}
}
Did it compile?
line 4: main() should be int main()?
another thing: sr = 1. 1 modulo any number is 1.
and finally. sr will never be equal to num2, because sr is 1 and num2 is 2 or greater so it will never print anything.
this will get you into an infinite loop that does nothing
If you wish to optimize it, you should use something like the sieve of eratosthenes.It is easy to operate on your data range. You can read more about it here :
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
#include <stdio.h>
#include <math.h>
int main(){
int num, sr, num2;
int isPrime = 1; // this is a check parameter; isPrime = 0 if number is not prime.
for(num=2; num<=100; num++){
sr = (int) sqrt(num);
for(num2=2; num2 <= sr; num2++){
//num2 <== sr to stop the innner loop
if(num%num2 == 0){
//printf("=========== %d|%d\n", num,num2); // uncomment this to see if a number is divisable
isPrime = 0; // this number is not prime, cos num can be divided by num2
break;
}
}
if(isPrime){
printf("Prime number is %d\n", num);
isPrime = 1; // reset the check parameter
}else{
isPrime = 1; // reset the check parameter
}
}
return 0;
}
This code works. Since it works, i'll let you play with it and optimize it. If you can't let us know. We'll try to help you.
I like how you used sqrt to optimize the code.
One optimization that you can use is the fact that all primes above 3 are of the form 6n+1 or 6n-1 and the fact that if a number is divisible by a prime, it is not a prime. Here is some code that uses that fact:
#include <stdio.h>
#include <math.h>
int is_prime(long num)
{
int k = 1, a = 0, b = 0;
long sr;
switch(num)
{
case 1: return 0;
case 2: return 1;
case 3: return 1;
case 4: return 0;
case 5: return 1;
case 6: return 0;
case 7: return 1;
}
if (num % 2 == 0) return 0;
if (num % 3 == 0) return 0;
sr = (int) sqrt(num);
while (b < sr) {
a = (6 * k) - 1;
b = (6 * k) + 1;
if (num % a == 0)
return 0;
if (num % b == 0)
return 0;
k += 1;
}
return 1;
}
void main(void)
{
int j;
for (j = 0; j<100; j++){
if (is_prime(j))
printf("%d is a prime\n", j);
}
}
This function returns 1 if num is a prime.

Finding largest prime factor of a composite number in c

I am accepting a composite number as an input. I want to print all its factors and also the largest prime factor of that number. I have written the following code. It is working perfectly ok till the number 51. But if any number greater than 51 is inputted, wrong output is shown. how can I correct my code?
#include<stdio.h>
void main()
{
int i, j, b=2, c;
printf("\nEnter a composite number: ");
scanf("%d", &c);
printf("Factors: ");
for(i=1; i<=c/2; i++)
{
if(c%i==0)
{
printf("%d ", i);
for(j=1; j<=i; j++)
{
if(i%j > 0)
{
b = i;
}
if(b%3==0)
b = 3;
else if(b%2==0)
b = 2;
else if(b%5==0)
b = 5;
}
}
}
printf("%d\nLargest prime factor: %d\n", c, b);
}
This is a bit of a spoiler, so if you want to solve this yourself, don't read this yet :). I'll try to provide hints in order of succession, so you can read each hint in order, and if you need more hints, move to the next hint, etc.
Hint #1:
If divisor is a divisor of n, then n / divisor is also a divisor of n. For example, 100 / 2 = 50 with remainder 0, so 2 is a divisor of 100. But this also means that 50 is a divisor of 100.
Hint #2
Given Hint #1, what this means is that we can loop from i = 2 to i*i <= n when checking for prime factors. For example, if we are checking the number 100, then we only have to loop to 10 (10*10 is <= 100) because by using hint #1, we will get all the factors. That is:
100 / 2 = 50, so 2 and 50 are factors
100 / 5 = 20, so 5 and 20 are factors
100 / 10 = 10, so 10 is a factor
Hint #3
Since we only care about prime factors for n, it's sufficient to just find the first factor of n, call it divisor, and then we can recursively find the other factors for n / divisor. We can use a sieve approach and mark off the factors as we find them.
Hint #4
Sample solution in C:
bool factors[100000];
void getprimefactors(int n) {
// 0 and 1 are not prime
if (n == 0 || n == 1) return;
// find smallest number >= 2 that is a divisor of n (it will be a prime number)
int divisor = 0;
for(int i = 2; i*i <= n; ++i) {
if (n % i == 0) {
divisor = i;
break;
}
}
if (divisor == 0) {
// we didn't find a divisor, so n is prime
factors[n] = true;
return;
}
// we found a divisor
factors[divisor] = true;
getprimefactors(n / divisor);
}
int main() {
memset(factors,false,sizeof factors);
int f = 1234;
getprimefactors(f);
int largest;
printf("prime factors for %d:\n",f);
for(int i = 2; i <= f/2; ++i) {
if (factors[i]) {
printf("%d\n",i);
largest = i;
}
}
printf("largest prime factor is %d\n",largest);
return 0;
}
Output:
---------- Capture Output ----------
> "c:\windows\system32\cmd.exe" /c c:\temp\temp.exe
prime factors for 1234:
2
617
largest prime factor is 617
> Terminated with exit code 0.
I presume you're doing this to learn, so I hope you don't mind a hint.
I'd start by stepping through your algorithm on a number that fails. Does this show you where the error is?
You need to recode so that your code finds all the prime numbers of a given number, instead of just calculating for the prime numbers 2,3, and 5. In other words, your code can only work with the number you are calculating is a prime number or is a multiple of 2, 3, or 5. But 7, 11, 13, 17, 19 are also prime numbers--so your code should simply work by finding all factors of a particular number and return the largest factor that is not further divisible.
Really, this is very slow for all but the smallest numbers (below, say, 100,000). Try finding just the prime factors of the number:
#include <cmath>
void addfactor(int n) {
printf ("%d\n", n);
}
int main()
{
int d;
int s;
int c = 1234567;
while (!(c&1)) {
addfactor(2);
c >>= 1;
}
while (c%3 == 0) {
addfactor(3);
c /= 3;
}
s = (int)sqrt(c + 0.5);
for (d = 5; d <= s;) {
while (c % d == 0) {
addfactor(d);
c /= d;
s = (int)sqrt(c + 0.5);
}
d += 2;
while (c % d == 0) {
addfactor(d);
c /= d;
s = (int)sqrt(c + 0.5);
}
d += 4;
}
if (c > 1)
addfactor(c);
return 0;
}
where addfactor is some kind of macro that adds the factor to a list of prime factors. Once you have these, you can construct a list of all the factors of the number.
This is dramatically faster than the other code snippets posted here. For a random input like 10597959011, my code would take something like 2000 bit operations plus 1000 more to re-constitute the divisors, while the others would take billions of operations. It's the difference between 'instant' and a minute in that case.
Simplification to dcp's answer(in an iterative way):
#include <stdio.h>
void factorize_and_print(unsigned long number) {
unsigned long factor;
for(factor = 2; number > 1; factor++) {
while(number % factor == 0) {
number = number / factor;
printf("%lu\n",factor);
}
}
}
/* example main */
int main(int argc,char** argv) {
if(argc >= 2) {
long number = atol(argv[1]);
factorize_and_print(number);
} else {
printf("Usage: %s <number>%<number> is unsigned long", argv[0]);
}
}
Note: There is a number parsing bug here that is not getting the number in argv correctly.

Resources