FizzBuzz Test

I do not see what is this buzz about this FizzBuzz Test. It is very simple to code. Fizzbuzz test is as follows
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz“. For numbers which are multiples of both three and five print “FizzBuzz“.

Here are my solutions

#include <iostream>

int main ( int argc, char **argv) {
	for ( int i = 0; i <= 100; i++) {
		if (  ( i % 3 == 0 ) && (i % 5 == 0 )) {
			std::cout << "FizzBuzz" << std::endl;
		}
		else if ( i % 3 == 0 ) {
			std::cout << "Fizz" << std::endl;
		}
		else if ( i % 5  == 0 ) {
			std::cout << "Buzz" << std::endl;
		} else {
			std::cout << i << std::endl;
		}
	}
	return 0;
}

First if condition can be changed to one

#include <iostream>

int main ( int argc, char **argv) {
	for ( int i = 0; i <= 100; i++) {
		if ( i % 15 == 0 ) {
			std::cout << "FizzBuzz" << std::endl;
		}
		else if ( i % 3 == 0 ) {
			std::cout << "Fizz" << std::endl;
		}
		else if ( i % 5  == 0 ) {
			std::cout << "Buzz" << std::endl;
		} else {
			std::cout << i << std::endl;
		}
	}
	return 0;
}

Project Euler – Problem 7

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

#include <cstdio>
#include <cstring>

#define MAX 1000000
bool p[MAX];
int main(void){

	memset(p, 1, sizeof(p));
	int ans = 1000, c = 1;
	for(int i = 4; i < MAX; i += 2) p[i] = 0;
	for(int i = 3; i < 1001; i += 2)
		if(p[i]){
			++c;
			for(int j = i * i; j < MAX; j += i) p[j] = 0;
		}
		while(c < 10001) c += p[++ans];

		printf("Answer: %d\n", ans);
		return 0;
}

Project Euler – Problem 4

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.

#include <cstdio>
int reverse(int x){
	int y = x, z = 0;
	while(y){
		z = z * 10 + (y % 10);
		y /= 10;
	}
	return z;
}
int main(int argc, char **argv){

	int ans = 0;
	for(int a = 999; a > 1; --a)
		for(int b = a; b > 1; --b){
			int x = a * b, z = reverse(x);
			if(x == z && x > ans) ans = x;
		}
		printf("Answer: %d\n", ans);
		return 0;
}

Project Euler – Problem 3

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

#include <cstdio>
int main(int argc, char **argv){
	long long num = 600851475143LL;
	int ans = 0;
	for(int div = 3; ; div += 2)
		if(!(num % div)){
			do { num /= div; } while (!(num % div));
			if(num == 1){
				ans = div;
				break;
			}
		}		
		printf("Answer: %d\n", ans);
		return 0;
}