• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Cant find System that matches requirements

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
i want a system that does the following(i = input, o = output):

i-> o
1-> 1
2-> 2
3-> 4
...
26-> 26
but:
27-> 1
28-> 2
29-> 3
...
52-> 26
but:
53-> 1
..
And so on

i tried it with modulo(%) but i still couldnt do it.
As an example i tried i % 26, but that didnt work & I tried i %27 and that didnt work either
thank you in advance for helping me! <3
 
Saloon Keeper
Posts: 7175
166
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The % operator is indeed the way to go. Post the code you tried, so we can see what's going on.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that
27%26 = 1
and
26%26 = 0
So you will need to handle the case where the answer is zero differently if you want the answer to be 26.
 
Greenhorn
Posts: 8
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran into this exact problem years ago when I was a Fortran programmer. I think what you want is



It's a trick so I hope  giving away the answer is ok.
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not really a trick. The first thing to observe is that you want the numbers 1, 2, 3, ..., 26 to repeat. So you need "% 26" to make that happen (and not "% 25" and not "% 27"). But that produces 0, 1, 2, ..., 25 repeating. So you need to add 1 at the end to fix that. But then you find the results starting at 2 instead of 1. So you need to subtract 1 at the beginning to fix that. This results in your formula.

I've found that to devise this sort of transformation it helps to write things in columns and adjust things as necessary. Like so:

Input i%26
    1   1
    2   2
...
   25  25
   26   0

But we don't want the 0 in that row, we want 26 there. But you can't get 26 as the result of %26 so you have to add 1 at the end:

Input i%26+1
    1   2
    2   3
...
   25  26
   26   1

But now the results are offset so you have to subtract 1 at the beginning:

Input (i-1)%26+1
    1   1
    2   2
...
   25  25
   26  26

It looks easy the way I did it but in practice it takes a bit of futzing around before you get it right.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic