Skip to content

Instantly share code, notes, and snippets.

@imiskolee
Created September 21, 2015 14:19
Show Gist options
  • Save imiskolee/b7d9bc0dfebe634ea472 to your computer and use it in GitHub Desktop.
Save imiskolee/b7d9bc0dfebe634ea472 to your computer and use it in GitHub Desktop.
/***
* =================================================================== *
* -------------------- About Perfect Num Analytize ------------------ *
* =================================================================== *
* Author : Misko_Lee(imiskolee#at#gmail.com)
* Create At : 2015-09-21 22:17
***/
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h> // for gettimeofday
int main(int argc,char *argv[]){
long long end_num = atoll(argv[1]);
unsigned int perfect_count = 0,abundant_count = 0,deficient_count = 0;
struct timeval start, end;
gettimeofday( &start, NULL );
int i=1,j=1;
for(;i<end_num;i++){
unsigned int aliquot_sum = 0;
j = 1;
for(;j<i;j++){
if(i % j == 0){
aliquot_sum += j;
}
}
if(aliquot_sum == i){
perfect_count++;
}else if(aliquot_sum > i){
abundant_count++;
}else{
deficient_count++;
}
}
gettimeofday( &end, NULL );
int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("\n\n");
printf("Range at 1..%lld\n", end_num);
printf("Perfect Num Count:%d\n", perfect_count);
printf("Abundant Num Count::%d\n", abundant_count);
printf("Deficient Num Count:%d\n", deficient_count);
printf("Real Complete Time:%fms\n", (double)timeuse / 1000);
}
<?php
/***
* =================================================================== *
* -------------------- About Perfect Num Analytize ------------------ *
* =================================================================== *
* Author : Misko_Lee(imiskolee#at#gmail.com)
* Create At : 2015-09-21 22:17
***/
$start_time = microtime(true);
$perfect_count = 0;
$abundant_count = 0;
$deficient_count = 0;
$end_num = $argv[1];
for($i=1;$i<$end_num;$i++){
$aliquot_sum = 0;
for($j=1;$j<$i;$j++){
if($i % $j == 0){
$aliquot_sum += $j;
}
}
if($aliquot_sum == $i){
$perfect_count++;
}else if($aliquot_sum > $i){
$abundant_count++;
}else{
$deficient_count++;
}
}
$end_time = microtime(true);
echo "\n";
echo "Ranage at 1 ..".$end_num."\n";
echo "Perfect Num Count:".$perfect_count."\n";
echo "Abundant Num Count:".$abundant_count."\n";
echo "Deficient Num Count:".$deficient_count."\n";
echo "Real Complete Time:".(($end_time - $start_time) * 1000) .'ms'."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment