Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 2:14pm On May 05, 2015*. Modified: 8:03am On May 06, 2015 |
kudaisi: The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
1: 1 3: 1,3 6: 1,2,3,6 10: 1,2,5,10 15: 1,3,5,15 21: 1,3,7,21 28: 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors? Editing ... |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 11:50am On May 05, 2015 |
kudaisi: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million. /** * * @author olyjosh */ public class SumOfPrime { public static void main(String[] args) { int max = 2000000;
//I implemented Sieve by taking true to be false n vice-versa, Just to avoid initial looping/flagging bla-bla boolean[] isPrime = new boolean[max]; for (int i = 2; i*i < max; i++) { if (!isPrime[i]) { for (int j = i; i*j < max; j++) { isPrime[i*j] = true; } } }
long sum = 0; for (int i = 2; i < max; i++) { if (!isPrime[i]) sum+=i; } System.out.println("Sum of prime <" + max + " is " + sum); } }
Sum of prime <2000000 is 142913828922 |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 2:20am On May 05, 2015 |
author=olyjosh post=33427863] /** * * @author olyjosh */ public class PythagoreanTriplet { public static void main(String[] args) { int lim=1000/3; for (int i = 2; i <=lim; i++) { for (int j = i+1; j <= lim; j++) { int c = j*j+i*i, b=j*j-i*i, a=2*i*j; if(1000==a+b+c)System.out.println("product: "+(a*b*c)); } } } }
product: 31875000[/quote]Quite effective, check it out here http://ideone.com/Y3B3wPRuntime: 0seconds Memory; 0kb |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 12:18am On May 05, 2015 |
kudaisi: A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
(see image for inserts) For example, (see image for inserts)
There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. /** * * @author olyjosh */ public class PythagoreanTriplet { public static void main(String[] args) { int lim=1000/3; for (int i = 2; i <=lim; i++) { for (int j = i+1; j <= lim; j++) { int c = j*j+i*i, b=j*j-i*i, a=2*i*j; if(1000==a+b+c)System.out.println("product: "+(a*b*c)); } } } }
product: 31875000 |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 11:19am On May 04, 2015 |
blueyedgeek:
(function () { var sum = 0; for (var i = 0; i <= 100; i += 1) { sum += i; } return Math.pow(sum, 2); } ()) - (function () { var sum = 0; for (var i = 0; i <= 100; i += 1) { sum += Math.pow(i, 2); } return sum; } () ) // 25164150
Cool implementation bro. But the best approach to this isn't greedy approach. The sum arithmetic progression is what you can use in both case. These are formula you can easily derive using mathematical inductions. Sn = {n(1+n)}/2 Sum of squres in AP is = {n(n+1)(2*n+1)}/6 where one 1 can alway be the first term if your series does not start from 1 and n is the last term |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 12:34pm On May 03, 2015 |
Sorry I wish I could delete the above implementation, It's buggy, BRB when I fix it |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 11:30am On May 03, 2015 |
kudaisi: 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? public class _10001stPrime { static boolean isPrime(long n) { if ( n % 2 == 0 )return false; long sqrt = (long) Math.sqrt(n); sqrt= sqrt%2 == 0 ? sqrt-1 : sqrt; for ( int i = 3; i < sqrt; i += 2 ) if ( n%i == 0 )return false; return true; } public static void main(String[] args) { int i =1; int n =3; while(i<10001){ if(isPrime(n))i++; n+=2; } System.out.println("10001st prime number: "+n); } } 10001st prime number: 103575I out for now - Gat exams to write tommorow. BRB After. |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 10:53am On May 03, 2015 |
kudaisi: The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385 The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025 Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
/** * * @author olyjosh */ public class DifferenceSumsSquare { public static void main(String[] args) { final int n=100; int sqOfSum = (n*(n+1)*(2*n+1))/6; int sumOfsq = (n*(1+n))/2; sumOfsq*=sumOfsq; System.out.println("Difference is "+(sumOfsq-sqOfSum)); } }
Difference is 25164150 |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 9:07am On May 03, 2015 |
Jregz: in php function getSum($u){ for ($uu = 0 ; $uu < 1000 ; $uu++){ if ($uu % 3 === 0 || $uu % 5 === 0 ){ $u += $uu ; } } return $u } echo getSum(0) ; // 233168 Welcome to the show |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 12:44am On May 03, 2015 |
@WhiZTiM check this out Runs for 10seconds on 4gb RAM, 1.65GHz Duo Core processor
public class Primes { static boolean isPrime(long n) { if ( n % 2 == 0 )return false; long sqrt = (long) Math.sqrt(n); sqrt= sqrt%2 == 0 ? sqrt-1 : sqrt; for ( int i = 3; i < sqrt; i += 2 ) { if ( n % i == 0 )return false; } return true; } public static void main(String[] args) {
long t = 600851475143L; long d = 2; while (1==1) { long tmp = 600851475143L / d; if ( t % tmp == 0 && isPrime(tmp) ) { System.out.println("= " + tmp); break; } d++; } } }
|
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 3:59pm On May 02, 2015 |
WhiZTiM: Comparing the Runtime of olyjosh's simple and elegant solution, (Java) with mine, (Python) You can see some abominable things happening... Python 3x faster than Java. ....HOw? Memorization. ....
http://ideone.com/XNVT5m ....Java (greedy) ...0.07seconds 320MB http://ideone.com/VSTUQe .....Python (Dynamic Programming) ...0.02seconds 8MB
Java will regain its glory if DP/Memorization is used. Cool. I will tryimplementing this with DP. Haaaa, but I doubt if i understand most of this python Types and sytax correctly. |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 3:29am On May 02, 2015 |
WhiZTiM: @olyjosh. I am really impressed! ...Would love to chat up with you Sir. Nice meeting you Bro |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 3:26am On May 02, 2015 |
WhiZTiM: . . . .Happy Coding .... . . .As for the prime number generation, you may want to try Sieve of Eratosthenes... or other Primality tests...
...my favorite, "...most prime numbers from 3 and above obeys ....(6k + 1) or (6k - 1), where k is a natural number"... THat should speed up implementation to some extent.... Thanks for reminding me of Sieve of Eratosthenes. That will work fine for the example test case but not for natural number that is as big as 600851475143, it wont work in java since the largest lenght of array or Set(and Set implementation) you can have is 2^31 (int precision) and Sieve of Eratosthenes reqires boolean flagging over such array. |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 3:10am On May 02, 2015 |
tohero: If u mean - will I b using, I wil say php, python nd java. Each at a time
I will b glad to hear ur opinion. I think you should go with any of C,C++,C#,python or java. Php is a script, I don't think it has such capability of doing trivial algorithmic problem like this. Someone should please correct me if I'm wrong |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 11:40pm On May 01, 2015 |
public class SmallestPositive { public static void main(String[] arg){ int num, x = 20; boolean isTrue=false; for (num = x; !isTrue; num+=x) { int i=0; for (i = 2; i <x; i++) { if(num%i!=0)break; else if(i==x-1)isTrue=true; } if(isTrue)break; } System.out.println("smallest positive number that is evenly divisible by all of the numbers from 1 to 20 is "+num); } }
smallest positive number that is evenly divisible by all of the numbers from 1 to 20 is 232792560 |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 5:05pm On May 01, 2015 |
Yeah, that will make more sense if there is a way to generate palindromes first. But my implementations generates product n test if it's palindrome from highest to lowest like you said. Can u post a code or algorithmic representation so I can get you right. |
Programming › Re: What are The Criteria For Closing A Thread On Nairaland ? by olyjosh(m): 12:34pm On May 01, 2015 |
@Kudaisi, you may be bann for 50years for posting this. I guess somebody shouldn't start treaking yet from sokoto, cause moderator still dey sleep now, e never see this post otherwise na 10 minutes to Calaba na en you go see #THREAD CLOSED |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 12:16pm On May 01, 2015 |
[quote author=olyjosh post=33308066]Sorry it not upto a second Logging Shows its under 20 milli-seconds |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 12:05pm On May 01, 2015 |
tohero: I can easily formulate the algorithm but writing them with a language is what I am yet to know Yeah, What language do you use in coding; |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 11:57am On May 01, 2015 |
lordZOUGA: How long did this code run? 2 seconds on my 4gb RAM, 1.65GHz processor. Any better solution without nested loop? |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 11:34pm On Apr 30, 2015 |
tohero: Intresting! Unfortunately there are limitations to my contribution Limitations, How ? |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 11:31pm On Apr 30, 2015 |
largest palindrome made from the product of two 3-digit numbers.
public class LargestPalindrome2_3 { static boolean isPalindrome(String s){ return new StringBuffer(s).reverse().toString().equals(s) ; } public static void main(String[] args) { int max = 999; int min =100; int prod=0; boolean isTrue = false; for (int i = max; i >=min ; i--) { for (int j = max; j >= min; j--) { prod=i*j; if(isPalindrome(String.valueOf(prod))){ isTrue=true; break; } } if(isTrue)break; } System.out.println("largest palindrome made from the product of two 3-digit numbers is:"+prod) ; } }
largest palindrome made from the product of two 3-digit numbers is:580085 |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 10:40pm On Apr 30, 2015 |
public class LargestPrimeFactor { static boolean isPrime(long n){ if(n==1)return false; if(n==2)return true; if(n%2==0) return false; int max = (int)Math.sqrt(n); for (int i = 2; i <= max; i++) { if(n%i==0)return false; } return true; } static Queue<Long> factors(long n){ Queue fac = new ArrayDeque(); //ArrayList<Long> factors = new ArrayList<>(); long h=Math.round(n/2); for (long i = 2; i <= h; i++) { if(n%i==0){fac.add(i);} } return fac; } public static void main(String[] args) { long lastPrime=0; long n =600851475143L; Queue<Long> factors = factors(n); for (Iterator<Long> iterator = factors.iterator(); iterator.hasNext()  { long next = iterator.next(); if(isPrime(next))lastPrime=next; } System.out.println("largest prime factor is: "+lastPrime); } } largest prime factor is: 6857This solution got me scared cause it runs for about 2minnutes on my 4gb RAM, 1.65GHz processor |
Programming › Re: Trivia Coding Questions (euler Project) by olyjosh(m): 8:56pm On Apr 30, 2015 |
Sum of even Fibonacci less than 4000000
public class FibonacciEven { public static void main(String[] args) { int num=4000000; int fib=0; int t2=2; int t1=1; int sum=2; while (fib<num) { fib=t1+t2; t1=t2;t2=fib; if(fib<num)if(fib%2==0)sum+=fib; } System.out.println(sum) ; } }
Answer : 4613732 |
Tech Jobs › Re: Programmer Needed To Program Naira Phone Payment System. by olyjosh(m): 7:09pm On Apr 26, 2015 |
Hi, You can reach me on 08061662025 or olyjoshone@gmail.com so we can talk more on that. I already developed ebook application for client and its hosted on playstore currently |
Programming › Re: how can i host Mysl Database Online by olyjosh(m): 5:24pm On Apr 21, 2015 |
The best thing is to do webservices app. In this approach you will have your mysql online and some script(php, jsp, aps etc) that interact with the database, hence your desktop app call these scripts(via url, HTTP connections). To do this with just java then you will need to do java hosting of your application.
You will need to read on: deploying javaEE, deploying RESTFUL Webservice or SOAP
And if you will be using php to querry your database the just try to know how to do RESTFUL webservice in php |
Programming › Re: Urgently Need Assistant On JAVA GUI by olyjosh(m): 9:14am On Apr 19, 2015 |
Are you getting error? Null pointer I guess. That is beacause variable u is null and you are passing it to float. What is u in your program |
Food › Re: Where Can I Buy Broccoli! In Nigeria? by olyjosh(m): 2:51am On Nov 23, 2014 |
|
Tech Jobs › Re: Personal Programming Tutor Needed Urgently. by olyjosh(m): 7:34am On Nov 11, 2014 |
|
Programming › Re: Programming advice by olyjosh(m): 9:59am On Oct 12, 2014 |
Guys, una harsh oo. You for ask am say en know whether en no go make hell. That guy woke up in dark and being dried of ideas |
Programming › Re: Sections-Most-Active-In Algorithm Is Wacked!! by olyjosh(m): 9:51am On Oct 12, 2014 |
What funnies me most is that every other time I use to see thing like, NairaLand need hard working programmer, designer..., then my expectation got rise like coming back another day to  say is this for real but seriously every other month I do say is this for real  cos I see no changes. Men, thing are not upto what they were suppose to be here. Like advert is all I see that is new thing here. I expect Nairaland to be one of those thing being discuss probably at silicon valley and most techi blogs/news |
Programming › Re: Why I Think NL Should Upgrade To HTML5 by olyjosh(m): 1:20am On Oct 08, 2014 |
Yungwizzzy: We'll look into it
thanks That's the only thing I'm tired of about suggestion given to guys at nairaland. Anyway to my own knowledge, I think Nairaland have very ugly codebase, or because its founded on an open source script that the developers are not flexible with, to change this might be very hard. However, they may need to wait for such implementation. Please quote me if 'm wrong, and correct me |