Solved 12

1 parent 6f501d4 commit 7477e028510ac6f21d8179d2b29c841bee05130f committed Nov 2, 2010
Showing with 80 additions and 34 deletions.
1. 12/a.out
2. +62 −17 12/solution.c
3. +18 −0 12/solution.py
4. +0 −17 12/solution.rb
BIN 12/a.out
Binary file not shown.
 @@ -8,36 +8,81 @@ using namespace std; int number_of_factors(unsigned long long * n) { // Store factors in an array. - unsigned long factors[500] = {0}; + int factors = 0; // No number has factors greater than the sqrt of itself. - unsigned long max = unsigned long (sqrt ((long double)*n)); + long long max = sqrt((long long)(*n)) + 1; - unsigned long test = ; - while (test < max) { - if (*n % *test == 0) { - - } else { - + long long x = 1; + // Scan through all possible factors. + while (x <= max) { + if (*n % x == 0) { + factors++; }; - - printf ("x = %d\n", x); - x++; + x++; } - + + return(factors); +} + +void find_max_triangle(){ + int factor_goal = 500; + + // Loop through all triangle numbers. + unsigned long long triangle = 1; + long last_addition = 1; + int factor_count = 0; + int max_factor_count = 0; + printf ("== Searching for a triangle number with 500 factors...\n\n"); + + while (factor_count < factor_goal) { + + last_addition ++; + triangle += last_addition; + + factor_count = number_of_factors(&triangle) ; + if (factor_count > max_factor_count) + max_factor_count = factor_count; + + if (last_addition % 10 == 0) + printf ("\r? %lld :: factors = %d :: max factor so far = %d :: last addition = %ld ", triangle, factor_count, max_factor_count, last_addition); + }; + + printf ("\n\n\n====== Found: %lld :: factors = %d :: last addition = %ld \n\n", triangle, factor_count, last_addition); - for (int i=3; i <= max; i+=2) { - if (*n % i == 0) - return(false); - } - return(true); } +unsigned long long triangle_for(int * count){ + unsigned long long triangle = 1; + long last_addition = 1; + + for (int i=1; i <= *count; i++){ + last_addition ++; + triangle += last_addition; + }; + + return triangle; + +} + int main (int argc, char *argv[]) { + find_max_triangle(); + /* + + int count = 0; + + unsigned long long triangle = 76576500; //triangle_for(&count); + int factor_num = number_of_factors(&triangle); + + printf ("\n\n=== %lld is the triangle for %d iterations.\n\n", triangle, count); + + printf ("\n\n== %lld has %d factors. \n\n", triangle, factor_num); + +*/ return(0); }
 @@ -0,0 +1,18 @@ +import math +def factors(n): + factors = 0 + for x in xrange(1, int(math.sqrt(n)+1)): + if n%x == 0: + factors += 2 + return factors + +def get_answers(): + a = 0 + triangle = 0 + while True: + a += 1 + triangle += a + if factors(triangle) > 500: + return triangle + False +print get_answers()
 @@ -1,17 +0,0 @@ -#!/usr/bin/env ruby -# coding: utf-8 -# ----------------------------------------- -# | A solution to a Project Euler problem | -# ----------------------------------------- -require 'rubygems' -require 'pp' -problem_no = File.dirname(File.expand_path(__FILE__)).split('/').last -puts "=== Solving Project Euler problem ##{problem_no}\n\n" -question = %Q{ - - - -} -puts "? #{question.strip}\n\n" -# -------------------------------------------------------------------- -