Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,160,458 members, 7,843,395 topics. Date: Wednesday, 29 May 2024 at 01:48 AM

Programming Challenge. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Programming Challenge. (2253 Views)

Programming Challenge For Beginners Competition Two N20000 -SEASON 2- / Programming Challenge For Beginners N20000 / Facebook Programming Challenge Question (2) (3) (4)

(1) (2) (Reply) (Go Down)

Programming Challenge. by Mobinga: 8:10am On Jul 23, 2011
This is very very simple. However I want to see how some of us code.

Write a program that outputs multiples of seven.  shocked shocked Easy innit?  Stop at 350 (50th multiple).

Write your code here and not the output. Preferably java.

Mod :: Using for loops only

The shorter the code the better.
Re: Programming Challenge. by Nobody: 9:22am On Jul 23, 2011
Re: Programming Challenge. by Mobinga: 10:03am On Jul 23, 2011
Sorry, I forgot to add. Using for loops only.

Do you syntax-highlight manually? or has Seun added that feature?
Re: Programming Challenge. by Nobody: 10:35am On Jul 23, 2011
Re: Programming Challenge. by Fayimora(m): 10:42am On Jul 23, 2011
(0, 350).each do |x| 
    puts x if((x%7)==0)
end
Re: Programming Challenge. by Mobinga: 12:15pm On Jul 23, 2011
@fayimora. Using for loops! and Java!

@omo_to_dun

Nice, but, I expected your output line to pass only "i";

---

I'll code a php syntax highlighter so we'd use in this section.
Re: Programming Challenge. by Fayimora(m): 12:26pm On Jul 23, 2011
Well omo_to_dun has that already, lol No point re-doing it
Re: Programming Challenge. by Mobinga: 12:28pm On Jul 23, 2011
The syntax highlighter or the code?

This should be your output line System.out.println(i);
Re: Programming Challenge. by Fayimora(m): 12:38pm On Jul 23, 2011
I mean he has the code already so no point doing it again
Re: Programming Challenge. by bakenda(m): 12:54pm On Jul 23, 2011
Complete PHP solution:


<?php
for($i=1;$i<=50;$i++)
{
$m=7*$i;
echo "$m<br/>";
}
?>


Easy does it. smiley
Re: Programming Challenge. by naijaswag1: 1:58pm On Jul 23, 2011
too trivial!
Re: Programming Challenge. by Mobinga: 2:10pm On Jul 23, 2011
bakenda:

Complete PHP solution:


<?php
for($i=1;$i<=50;$i++)
{
$m=7*$i;
echo "$m<br/>";
}
?>


Easy does it. smiley

Wrong. This code can be reduced in terms of everything.

naija_swag:

too trivial!

This is the sort of arrogance I dislike.
omo_to_dun which is a programming beast has attempted it. The essence of the challenge is not to get the answer but to see how concise our codes can be. Please attempt it before concluding.

Fayimora:

I mean he has the code already so no point doing it again
He hasn't simplified it to the last bit!



---
Simplicity.
Re: Programming Challenge. by Nobody: 6:51pm On Jul 23, 2011
Re: Programming Challenge. by GoodMuyis(m): 9:32pm On Jul 23, 2011
Doing it in PHP [while loop style]



<?PHP
$int = 7;

while($int < 350){
$int *= 7;
echo $int . ", ";
}

?>

Re: Programming Challenge. by GoodMuyis(m): 10:07pm On Jul 23, 2011
Point of Correction


<?PHP

for($int = 7; $int < 343; ){ // have difficulties in setting to 350 here
$int *=7;
echo $int . ", ";
}

?>
Re: Programming Challenge. by bakenda(m): 11:03pm On Jul 23, 2011
Here is a little modification of my previous code:


<?php
for($i=1;$i<=50;$i++)
{
echo 7*$i."<br/>";
}
?>


If you have the 'right' solution, it's time to show it-the world is waiting?
Re: Programming Challenge. by naijaswag1: 3:32am On Jul 24, 2011
Mobinga:

Wrong. This code can be reduced in terms of everything.

This is the sort of arrogance I dislike.
omo_to_dun which is a programming beast has attempted it. The essence of the challenge is not to get the answer but to see how concise our codes can be. Please attempt it before concluding.
He hasn't simplified it to the last bit!



---
Simplicity.

I have been doing a lot of algorithms lately and when I say that this is simple,it isnt arrogance.this is the kind of problems I will solve with my students while teaching.if you know something and you are quite sure of your self,it isnt arrogance and for that i am posting these code,I did in about 10mins after what you wrote got me angry to prove to myself that i wasnt bluffing.its too trivial and i will repeat it again.

public void multiples(){
for(int i=0;i<=350;i++)
if(i%7==0)
System.out.print(i + " "wink;
}
Re: Programming Challenge. by Mobinga: 7:40am On Jul 24, 2011
Quite frankly onluy omo_to_dun has done it the simplest way possible. There could be simpler ways but I think this is the most simple and efficient

      for(short i = 0; i <= 343; i += 7)
         System.out.println(i);
Re: Programming Challenge. by Mobinga: 7:53am On Jul 24, 2011
bakenda:

Here is a little modification of my previous code:


<?php
for($i=1;$i<=50;$i++)
{
echo 7*$i."<br/>";
}
?>


If you have the 'right' solution, it's time to show it-the world is waiting?

Haha see a simplified version of your code

<?php
for($i=0;$i<=350;$i+=7)
{
echo $i."<br/>";
}
?>



I know some of us are used to using i++ as our increment statement. . .then within the for loops blocks we'd now use the modulus operator. That's not clean. As it loops through each one . . . 1, 2 , 3 to  350

You can also pass a numeric value (int oh no go pass 3.434 ) as the increment statement i+=7 << The variable increments in 7's. This optimizes your code and allocates less memory. This can be particularly useful when working on big projects.

Nothing is too trivial. We can all learn. cool
Re: Programming Challenge. by bakenda(m): 1:06pm On Jul 24, 2011
Mobinga:

Haha see a simplified version of your code

<?php
for($i=0;$i<=350;$i+=7)
{
echo $i."<br/>";
}
?>



I know some of us are used to using i++ as our increment statement. . .then within the for loops blocks we'd now use the modulus operator. That's not clean. As it loops through each one . . . 1, 2 , 3 to  350

You can also pass a numeric value (int oh no go pass 3.434 ) as the increment statement i+=7 << The variable increments in 7's. This optimizes your code and allocates less memory. This can be particularly useful when working on big projects.

Nothing is too trivial. We can all learn. cool

That code isn't simpler than my code, at best it's same.It's like you didn't notice that my loop terminates at 50 not 350,
though you used an increment of 7 , your loop terminates at 350----about the same number of cycles.
Re: Programming Challenge. by Mobinga: 8:40pm On Jul 24, 2011
bakenda:

That code isn't simpler than my code, at best it's same.It's like you didn't notice that my loop terminates at 50 not 350,
though you used an increment of 7 , your loop terminates at 350----about the same number of cycles.

+1. CPU's multiplication is basically addition.
Re: Programming Challenge. by Fayimora(m): 11:41pm On Jul 24, 2011
I think they are both the same cheesy and n another sense I think the multiplication is faster. Sounds weird ryt? The little assembly i did thought me to always do additions rather than multiplications, reason because its faster. However, i ran each of the code checking the runtime and the multiplication ran for an average of 0.82secs while the addition ran for an average of 0.86.

The fastest way would just be to add, concatenate and print once. If the max value grows less than 350(say 1000000) then you can still make it faster by using a StringBuilder or if your doing anything with concurrency then a StringBuffer,

Just an opinion by the way,
Re: Programming Challenge. by Mobinga: 10:08am On Jul 25, 2011
Nice. I think this is faster though tedious

System.out.println("0/n7/n14, "wink 



grin Since it doesn't loop at all. It just prints the string.


We should be doing code-benchmarks more often grin
Re: Programming Challenge. by GoodMuyis(m): 11:00am On Jul 25, 2011
naija_swag:

I have been doing a lot of algorithms lately and when I say that this is simple,it isnt arrogance.this is the kind of problems I will solve with my students while teaching.if you know something and you are quite sure of your self,it isnt arrogance and for that i am posting these code,I did in about 10mins after what you wrote got me angry to prove to myself that i wasnt bluffing.[s]its too trivial and i will repeat it again[/s].

public void multiples(){
for(int i=0;i<=350;i++)
if(i%7==0)
System.out.print(i + " "wink;
}

Well you defend yourself quite right, but as you are a teacher you shouldn't be angry since most of us here are student Try to learn English and coding skills, so you will definitely got this act from Student undecided

BTW good Code from you, i almost did the same in PHP but i don't understand whats the meaning Multiple until this morning
Re: Programming Challenge. by Mobinga: 12:27pm On Jul 25, 2011
naija_swag:

I have been doing a lot of algorithms lately and when I say that this is simple,it isnt arrogance.this is the kind of problems I will solve with my students while teaching.if you know something and you are quite sure of your self,it isnt arrogance and for that i am posting these code,I did in about 10mins after what you wrote got me angry to prove to myself that i wasnt bluffing.i[b]ts too trivial and i will repeat it again.[/b]

public void multiples(){
for(int i=0;i<=350;i++)
if(i%7==0)
System.out.print(i + " "wink;
}

It is not too trivial, I said in the simplest way possible. Yours is not the simplest way possible.

Your code loops from 0 to 350 with an increment of 1 in each cycle, It further checks if the incremented value is perfectly divisible by 7.

Your code is right logically, but it fails to meet the requirements of this challenge.
Re: Programming Challenge. by Fayimora(m): 3:20pm On Jul 25, 2011
Mobinga:

Nice. I think this is faster though tedious

System.out.println("0/n7/n14, "wink 



grin Since it doesn't loop at all. It just prints the string.


We should be doing code-benchmarks more often  grin

Hahah yeah but dnt forget its just 350. What happens if the max number is increased to 1million? lolz

offtopic: Mobinga u havent replied ma pm, sent it few days ago
Re: Programming Challenge. by netghost: 3:40pm On Jul 25, 2011
Mobinga:


This is the sort of arrogance I dislike.
omo_to_dun which is a programming beast has attempted it.


shocked shocked shocked hmmmm
Re: Programming Challenge. by iGravity(m): 1:02pm On Aug 03, 2011
@fayimora there is an error in your code.

Fayimora:

(0, 350).each do |x| 
    puts x if((x%7)==0)
end


You should use (0, 350).each instead of (0, 350) which fails.




The way I would do this is,

1.upto(350) do |x|
    puts x if x.divisible_by? 7
end

# re-open the Fixnum class to add our new behaviour
class Fixnum
    def divisible_by? num
        self % num == 0
    end
end


Please audit and point out the bugs as I did this without IRB to help me out.
Re: Programming Challenge. by Fayimora(m): 5:05pm On Aug 03, 2011
HAhahahaha there ain't no error, Its just Nairaland, It doesn't allow multiple dots. cheesy
Re: Programming Challenge. by iGravity(m): 8:16pm On Aug 03, 2011
Lmao! Wow I didn't see this. What sort of thing "runs" this forum? Anyways, all good. You were right in the end
Re: Programming Challenge. by skydancer: 5:34pm On Aug 04, 2011
Hmm, Mobinga in your anger, you turned arrogant as well. It happened that bakenda's 'simplified' php code was better than yours in the end. Anyway, that's quite surprising and may be due to php server things, . It's possible there's an improved way of multiplying without repeated additions. I'll look that up later. , nice codes anyway.
Re: Programming Challenge. by Beaf: 9:25pm On Aug 04, 2011
It is really sad and upsetting that anyone would come up with such a cheap challenge. embarassed
Please dudes, either make sure to come up with brain crackers, or just ask for coding help.

(1) (2) (Reply)

I Need Advanced PHP Programming,ajax And JS Ebooks, Pls!!! / --theory: Geeks, Computer Hobbyists And Programmers Hardly Gets The Girl Quick? / How Can I Send Bulk SMS To MTN Numbers With My Sender Id

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 41
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.