aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-175/adam-russell/blog.txt1
-rw-r--r--challenge-175/adam-russell/java/ch-1.java37
-rw-r--r--challenge-175/adam-russell/java/ch-2.java65
-rw-r--r--challenge-175/adam-russell/perl/ch-1.pl31
-rw-r--r--challenge-175/adam-russell/perl/ch-2.pl42
5 files changed, 176 insertions, 0 deletions
diff --git a/challenge-175/adam-russell/blog.txt b/challenge-175/adam-russell/blog.txt
new file mode 100644
index 0000000000..9d1352d3f4
--- /dev/null
+++ b/challenge-175/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/07/30
diff --git a/challenge-175/adam-russell/java/ch-1.java b/challenge-175/adam-russell/java/ch-1.java
new file mode 100644
index 0000000000..8f12c0c363
--- /dev/null
+++ b/challenge-175/adam-russell/java/ch-1.java
@@ -0,0 +1,37 @@
+import java.util.Calendar;
+
+class LastSunday{
+ private static String lastSundayMonth(int month, int year){
+ int day = 20;
+ Calendar c = Calendar.getInstance();
+ c.set(year, month, day);
+ int lastSunday = day;
+ while(day <= c.getActualMaximum(Calendar.DATE)){
+ c.set(Calendar.DATE, day);
+ if(c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
+ lastSunday = day;
+ day++;
+ }
+ month++;
+ String m = String.valueOf(month);
+ if(month < 10)
+ m = "0" + m;
+ return year + "-" + m + "-" + lastSunday;
+ }
+
+ public static String[] lastSunday(int year){
+ int month = 0;
+ String lastSunday[] = new String[12];
+ while(month < 12){
+ lastSunday[month] = lastSundayMonth(month, year);
+ month++;
+ }
+ return lastSunday;
+ }
+
+ public static void main(String[] args){
+ String[] lastSunday = lastSunday(2022);
+ for(int i = 0; i < lastSunday.length; i++)
+ System.out.println(lastSunday[i]);
+ }
+}
diff --git a/challenge-175/adam-russell/java/ch-2.java b/challenge-175/adam-russell/java/ch-2.java
new file mode 100644
index 0000000000..15232709ce
--- /dev/null
+++ b/challenge-175/adam-russell/java/ch-2.java
@@ -0,0 +1,65 @@
+import java.util.HashSet;
+import java.util.ArrayList;
+
+class PerfectTotient{
+
+ private static double EPSILON = 1e-7;
+
+ private static ArrayList primeFactors(int n){
+ ArrayList factors = new ArrayList();
+ while (n % 2 == 0){
+ factors.add(new Integer(2));
+ n = n / 2;
+ }
+ for(int i = 3; i <= Math.sqrt(n); i = i + 2){
+ while (n % i == 0){
+ factors.add(new Integer(i));
+ n = n / i;
+ }
+ }
+ if(n > 2)
+ factors.add(new Integer(n));
+ return factors;
+ }
+
+ private static int totient(int n){
+ ArrayList factors = new ArrayList();
+ factors.addAll(new HashSet(primeFactors(n)));
+ float totient = n;
+ for(int i = 0; i < factors.size(); i++){
+ totient *= (1 - (1.0 / ((Integer) factors.get(i)).intValue()));
+ }
+ return (int) totient;
+ }
+
+ public static int[] perfectTotients(int n){
+ int[] perfectTotients = new int[n];
+ int i = 0;
+ int x = 1;
+ while(i < n){
+ x++;
+ ArrayList totients = new ArrayList();
+ int totient = totient(x);
+ totients.add(new Integer(totient));
+ while(Math.abs(totient - 1) > EPSILON){
+ totient = totient(totient);
+ totients.add(new Integer(totient));
+ }
+ int totientSum = 0;
+ for(int j = 0; j < totients.size(); j++)
+ totientSum += ((Integer)totients.get(j)).intValue();
+ if(totientSum == x){
+ perfectTotients[i] = x;
+ i++;
+ }
+ }
+ return perfectTotients;
+ }
+
+ public static void main(String[] args){
+ int[] perfectTotients = perfectTotients(20);
+ for(int i = 0; i < perfectTotients.length - 1; i++)
+ System.out.print(perfectTotients[i] + ", ");
+ System.out.println(perfectTotients[perfectTotients.length - 1]);
+ }
+}
diff --git a/challenge-175/adam-russell/perl/ch-1.pl b/challenge-175/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..be6d9a1582
--- /dev/null
+++ b/challenge-175/adam-russell/perl/ch-1.pl
@@ -0,0 +1,31 @@
+use strict;
+use warnings;
+##
+# Write a script to list the last sunday of every month in the given year.
+##
+use Time::Piece;
+
+sub last_sunday_month{
+ my($month, $year) = @_;
+ $month = "0$month" if $month < 10;
+ my $sunday;
+ my $t = Time::Piece->strptime("$month", "%m");
+ for my $day (20 .. $t->month_last_day()){
+ $t = Time::Piece->strptime("$day $month $year", "%d %m %Y");
+ $sunday = "$year-$month-$day" if $t->wday == 1;
+ }
+ return $sunday;
+}
+
+sub last_sunday{
+ my($year) = @_;
+ my @sundays;
+ for my $month (1 .. 12){
+ push @sundays, last_sunday_month($month, $year);
+ }
+ return @sundays;
+}
+
+MAIN:{
+ print join("\n", last_sunday(2022)) . "\n";
+}
diff --git a/challenge-175/adam-russell/perl/ch-2.pl b/challenge-175/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..2d1209acc6
--- /dev/null
+++ b/challenge-175/adam-russell/perl/ch-2.pl
@@ -0,0 +1,42 @@
+use strict;
+use warnings;
+##
+# Write a script to generate the first 20 Perfect Totient Numbers.
+##
+use constant EPSILON => 1e-7;
+
+sub distinct_prime_factors{
+ my $x = shift(@_);
+ my %factors;
+ for(my $y = 2; $y <= $x; $y++){
+ next if $x % $y;
+ $x /= $y;
+ $factors{$y} = undef;
+ redo;
+ }
+ return keys %factors;
+}
+
+sub n_perfect_totients{
+ my($n) = @_;
+ my $x = 1;
+ my @perfect_totients;
+ {
+ $x++;
+ my $totient = $x;
+ my @totients;
+ map {$totient *= (1 - (1 / $_))} distinct_prime_factors($x);
+ push @totients, $totient;
+ while(abs($totient - 1) > EPSILON){
+ map {$totient *= (1 - (1 / $_))} distinct_prime_factors($totient);
+ push @totients, $totient;
+ }
+ push @perfect_totients, $x if unpack("%32I*", pack("I*", @totients)) == $x;
+ redo if @perfect_totients < $n;
+ }
+ return @perfect_totients;
+}
+
+MAIN:{
+ print join(", ", n_perfect_totients(20)) . "\n";
+}